PDA

View Full Version : How to run a VBA macro from a custom toolbar button?



NigelFinch1
24th Nov 2004, 11:58 pm
Could you please tell me how to add a tool to a toolbar that will run a VBA
sub or function?

I have already created the VBA code and can run this from the tools-macro menu option, but I am having trouble associating this with a toolbar button.

I am also fine creating custom buttons that are associated with predefined
commands, but I do not know the 'macro code' for linking a particular VBA macro with a button. With Office applications such as Word or excel, you can select your macros from a list when adding a custom button.

I am using AutoCAD 2004.

Regards,

Nigel

hendie
25th Nov 2004, 09:49 am
there are a few ways but imo the "best" way is to assign the vba macro to a lisp, then you can just call the Lisp from the button.


(defun C:BLISO(/ oldecho)
(setq oldecho (getvar "cmdecho"))
(setvar "cmdecho" 0)
(command "-vbarun" "C:/Program Files/RCIapps/Bliso.dvb!BlockIsoMod1.bliso")
(setvar "cmdecho" oldecho)
(princ)
)
The you just need

^C^C_bliso; in the macro part of the tool button properties.
you could also just write the

-vbarun" "C:/Program ....... in the button but with the first example, if you hit enter again, the entire routine will repeat, whereas in the second, if you hit enter again you will be prompted for the macro name

Fantomas
25th Nov 2004, 04:13 pm
If your project in the 'Support File Search Path':

^C^C_.-vbarun;myProject.dvb!myModule.mySub;

If not:

^C^C_.-vbarun;C:/...../myProject.dvb!myModule.mySub;

NigelFinch1
22nd Jan 2005, 11:49 pm
Thanks very much... both solutions work.

Dommy2Hotty
24th Jan 2005, 04:44 pm
The you just need

^C^C_bliso; in the macro part of the tool button properties.

I'd prefer to see

^C^C^C(if (not c:bliso)(load "bliso));_bliso;

hsharghi
27th Jun 2006, 07:56 pm
I prefere this:
1) add your .dvb project file into AutoCAD startup suit (appload command)

2) write this simple LISP code:


(defun C:CustomFunctionName()
(vl-vbarun "FunctionNameInVBProject")
)

and add it to AutoCAD startup suit

3) you can add a shortcut command in acad.pgp to your CustomFunctionName or Add it to a custom toolbar.