AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Load VBA project from C#

1 REPLY 1
SOLVED
Reply
Message 1 of 2
Anonymous
824 Views, 1 Reply

Load VBA project from C#

Hi.

 

I am trying to load VBA project from my DLL library on C#, and I am trying the next code:

 

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.SendStringToExecute("(load \"C:\\\\project.dvb\") ", false, false, true);

 

How I can load my VBA project with C#?

 

Thanks beforehand,

1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: Anonymous

You can surely use SendStringToExecute() to load VBA project and run macro in it, provided you have the command input entered correctly. In your case, you use command "VBALOAD", not "LOAD".

 

You can also use AutoCAD COM API to load/run VBA/VBA Macro. The sample code below uses "dynamic", so that no reference to AutoCAD COM libraries are necessary:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(RunningVBA.Commands))]

namespace RunningVBA
{
    public class Commands 
    {
        private const string VBA_PATH = 
            @"C:\Users\norm\Documents\Visual Studio 2015\Projects\Acad NET\RunningVBA\MyVbaProject.dvb";
        private const string VBA_MACRO = @"MyVbaModule.MyVbaMacro";

        [CommandMethod("DocCmd")]
        public static void RunDocCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            dynamic comApp = CadApp.AcadApplication;

            comApp.LoadDVB(VBA_PATH);
            comApp.RunMacro(VBA_MACRO);
        }
    }
}

Then, this is the VBA code in module called "MyVbaModule", saved in MyVbaProject.dvb:

 

Option Explicit

Public Sub MyVbaMacro()

    ThisDrawing.Utility.Prompt vbCr & "MyVbaMacro: Hello World!"
    
End Sub

As you can see, it is fairly simple. HTH.

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report