load and run VBA macro from the command line

load and run VBA macro from the command line

a.kouchakzadeh
Advocate Advocate
5,366 Views
5 Replies
Message 1 of 6

load and run VBA macro from the command line

a.kouchakzadeh
Advocate
Advocate

Hello every one and Mr. Norman

 

I was wondering, is there anyway to load and run a vba macro just from the command line without using the dialog box?

I have this:

 

vbarun "C:/folder/CODES/P.dvb!M.test"

 

when I paste it into the command line, it opens up the Dialog box which I dont want to see it. I just want it to directly run without needing to press Enter.

Actually, what I have so far is a VB.net ribbon program, which uses a handler when buttons are pressed:

 

if(button.Text == "Pendent")
{
doc.SendStringToExecute("^C^C_.vbarun;C:/NSV/CODES/P.dvb!P.P", true, false, true);
}

 

whats wiered is this <^C^C_.vbarun;C:/NSV/CODES/P.dvb!P.P> works totally fine in the CUI.

But it does not work in the command line.

 

@norman.yuan 

0 Likes
Accepted solutions (2)
5,367 Views
5 Replies
Replies (5)
Message 2 of 6

Ed__Jobe
Mentor
Mentor
Accepted solution

You need to run it as a lisp command using the vla-runmacro command. See the lisp example at the bottom of the page I linked.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 6

Ed__Jobe
Mentor
Mentor

You can't send vba or other code to the command line, only commands and lisp.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor
Accepted solution

To run VBA code, with built-in commands, you need to load the *.dvb file first and then run it.  It looks like you want to start to run a VBA macro from a ribbon menu item created via .NET API, using Document.SendStringToExecute(). As far as I can see, the string content you passed to the SendStringToExecute() is missing a trailing space (" ") or a ";" (I usually use " "). That is, if you want to call a command "MyCommand", then the code should be:

dwg.SendStringToExecute("MyCommand ", ... ...).

 

Because you want to run a VBA macro, it would be better to use this lisp function:

(vl-vbarun "myVbaProject.dvb!moduleName.MacroName")

 

With this lisp function:

1.  you can either prefix the file path of the DVB file, or you can leave the path out, as long as the DVB file can be find in one of the support paths of current user profile.

 

2. you do not have to care if the DVB file is loaded or not. this function would load the DVB file first, if it has not been loaded.

 

So, the code for your .NET ribbon ICommand would look like:

 

doc.SendStringToExecute("(vl-vbarun \"TheVBA.dvb!TheModule.TheMacro\") ", true, false, true);

 

It might be better to define a Lisp command that runs the VBA macro:

(defun c:MacroCmd ()

   (vl-vbarun "[paht/file].dbv!module.macro")

)

and then include this lisp command in, say, acad.lsp, so that it is always loaded.

 

Then in your .NET code, where SendStringToExecute() is called, you simply pass the command name "MacroCmd " (notice the trailing space). The advantage of using this approach is, you can update/change the VBA file/code freely without having to recompile your .NET code, as long as the list defined command "MacroCmd" is not changed. 

 

I guess you are in a transition stage of moving to .NET API development, but have many existing functionalities in VBA code. Using a Lisp-defined command in between your .NET app and VBA app, so they are loosely decoupled, would make the transition more flexible.

 

HTH.

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 6

a.kouchakzadeh
Advocate
Advocate

Thank you Sir

it works perfect and great.

Yes, your guess is correct. I'm migrating to VB.NET and since I don't have a solid background in programming, its being a little bit hard to figure out what to do. specially that I couldn't find a good source to make progress. Mastering AutoCAD VBA by Cottingham was a good help when I started VBA a couple of months ago. but I couldn't find anything similar in VB.net so far.

0 Likes
Message 6 of 6

norman.yuan
Mentor
Mentor
If you just started moving to AutoCAD .NET API, and also still new to MS .NET in general, it would be better choice to use C# as the .NET programming language than VB.NET. While you may think VB.NET might be the natural choice because of the similar language syntax, I'd say it is a disadvantage, rather than advantage: you may bring in many bad code habits from VBA's "not-so-much OO" approaches. Furthermore, VB.NET will not be supported/evolved along with future MS .NET technology. While this does not impact current AutoCAD .NET API, but who knows the future. Also, there are a lot more AutoCAD .NET API resources/sample code online in C# than VB.NET.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes