Calling VBA macro from command line or button

Calling VBA macro from command line or button

Anonymous
Not applicable
13,677 Views
4 Replies
Message 1 of 5

Calling VBA macro from command line or button

Anonymous
Not applicable

I'm a little new to VBA programming and customization and I'm having trouble with something.  I wrote a VBA macro (in its own module) but the only I know to call it is with the run VBA dialog box (VBARUN command) selecting it from the list.

 

I am wondering if there is a way to create a unique command name for it so I can call it directly from the command line, or if I can create a button to call it from my custom tool pallette.  Thanks for any help.

 

Caleb B

0 Likes
13,678 Views
4 Replies
Replies (4)
Message 2 of 5

andrewpuller3811
Advisor
Advisor
(defun C:HB ()
(vl-load-com)
(vl-vbarun "HotButton2010.dvb!HBMain")
(princ)
)

 

I have the above in my acaddoc.lsp

 

This defines a command HB. When run on the command line it loads the vba file HotButton2010.dvb and run the sub HBMain from within that file.

 

In this case the acaddoc.lsp and vba file are stored in one of autocads support folders, preferrably the first folder in the list.

 

Before you get too far into vba, you might want to look at vb .net, or c#.net as vba in autocad is on it's way out.

As far as I know, 2011 is the last version to support vba.



If a post provides a fix for your issue, click on "Accept as Solution" to help other users find solutions to problems they might have that are similar to yours.

Andrew Puller
Maitland, NSW, Australia
Windows 11
Intel core i7 11800 @ 2.30 GHz with 32GB Ram
Civil 3d 2023
0 Likes
Message 3 of 5

Anonymous
Not applicable

Put the following in the Macro definition of a toolbar button:

 

^C^C_.-vbarun;"C:/MyPath/MyMacroFilename.dvb!MyModuleName.MyRoutineName";

Message 4 of 5

Anonymous
Not applicable

This is also what I use, however I just came across a few toolbar buttons that weren't working when I changed to a newer directory.  I was confused and frusterated for a while.  The last thing I tried was to shorten the lengths of the sub procedure names, and it worked!

 

I have a feeling there is some path length limitation for the path in quotes below, however I could be wrong I'm only guessing.  The path length of 111 characters worked for me, but the length of 133 characters didn't.

 

^C^C_.-vbarun;"C:/MyPath/MyMacroFilename.dvb!MyMod​uleName.MyRoutineName";

0 Likes
Message 5 of 5

Anonymous
Not applicable

The -vbarun; etc. line is how to do it. You should know that it is possible to pass arguments in that very same line to your VBA. Thus you can have that VBA act upon the arguments passed to it. So keep that in mind as a way to leverage your VBA functionality.

0 Likes