Load VBA script in AutoCAD

Load VBA script in AutoCAD

MRCH94D9W
Enthusiast Enthusiast
2,508 Views
11 Replies
Message 1 of 12

Load VBA script in AutoCAD

MRCH94D9W
Enthusiast
Enthusiast

I'm trying to make AutoCAD run a VBA script, but I can't seem to load the script properly.

 

Sub hello()
MsgBox ("Hello World")
End Sub

 

This runs perfectly when I execute the script.


I save the script as hello.dvb


Now, when I close the drawing and reopen it, my VBA script is gone from the editor.


Trying to reload it, using the command VBALOAD and loading hello.dvb into the drawing should allow me to run the script again, but looking in the VBA editor, my script has not been loaded.


Am I doing something wrong here?

0 Likes
Accepted solutions (1)
2,509 Views
11 Replies
Replies (11)
Message 2 of 12

MRCH94D9W
Enthusiast
Enthusiast

bump

0 Likes
Message 3 of 12

norman.yuan
Mentor
Mentor

@MRCH94D9W wrote:

....

Trying to reload it, using the command VBALOAD and loading hello.dvb into the drawing should allow me to run the script again, but looking in the VBA editor, my script has not been loaded.


Am I doing something wrong here?


Are you sure you actually loaded the *.dvb file (you might have clicked the "Cancel" button unknowingly))? One way to see if the *.dvb file is loaded or not, or to load the *dvb file, is to start VBA Manager with command "VBAMAN". You can see if a *.dvb file is loaded or not, and if not, you can click "Load..." button to load.

 

normanyuan_2-1714401524318.png

 

That is, the situation as you described (reloaded, but the VBA editor did not) cannot happen: the *.dvb file either loaded, or not loaded.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 12

MRCH94D9W
Enthusiast
Enthusiast

I've tried loading it with both VBALOAD and VBAMAN, but it simply doesn't load the script.

Perhaps there is some setting that enables VBA that I'm missing?

 

Please see attached video

0 Likes
Message 5 of 12

norman.yuan
Mentor
Mentor

Do you have other version of AutoCAD installed? If yes, does VBA project load there?

Try following to see what happens:

1. reset AutoCAD settings to defaults. Window start menu-all apps->Autodesk->AutoCAD202X->Reset settings to defaults

2. try to uninstall the VBA enabler and reinstall it

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 12

Ed__Jobe
Mentor
Mentor

@MRCH94D9W Also try this: After you import the module, run Debug>Compile to see if there are any compile errors. The project won't load if there are compile errors. It might be the line:

MsgBox ("Hello World!")

 

When a function is not called in a Set statement, there shouldn't be any parenthesis. It should just be:

MsgBox "Hello World!"

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 7 of 12

MRCH94D9W
Enthusiast
Enthusiast

I have now reinstalled the entire AutoCAD package, but the issue persists.

0 Likes
Message 8 of 12

MRCH94D9W
Enthusiast
Enthusiast

I've not used import / export module but saved it as a dvb file.

 

However, after your reply, I've tried to export the module as a bas file and import it again in the VBA editor, which works as intended. Please see the attached video.

 

What I'm working towards is adding a button to AutoCAD to run a certain script, and it would be preferable if I didn't have to import it every single time I open AutoCAD, but I'm not sure this is the way to go about it?

0 Likes
Message 9 of 12

Ed__Jobe
Mentor
Mentor

You don't export/import every time. That was just to fix a corrupted dvb. A dvb is comprised of not just the text you write, but also compiled code. Think of it like a zip file. Once you get a dvb compiled and working properly, you just load it and run the macro. There are two types of errors, Compile time and Run time. Compile errors are ones that prevent  your code from compiling. They can be spelling errors or syntax errors. To find these, do Debug>Compile like I mentioned last time. Runtime errors are errors in your logic that only occurs when the code is running. Once the code compiles, you can set a breakpoint and run the code in Debug mode and watch what happens one line at a time. You can inspect variable values and make sure the code executes as planned. Once you have all the errors found, you can issue the code to other users. The best way to do this is to create a partial menu cuix with your custom buttons/toolbars/ribbon panels. The partial menu can have an *.mnl lisp file. In that, you add a line that loads your dvb. See my tips on how to do this here.

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 10 of 12

MRCH94D9W
Enthusiast
Enthusiast
Accepted solution

I finally figured it out. It was simply because the directory was not added to Trusted Locations...

One would think AutoCAD would tell you that prompting the error, but unfortunately they'd rather keep you guessing for solutions.

 

0 Likes
Message 11 of 12

MRCH94D9W
Enthusiast
Enthusiast

Perfect, thank you.
The solution was autolisp rather than vba all along. Everything now works as it's supposed to 🙂

0 Likes
Message 12 of 12

Ed__Jobe
Mentor
Mentor

@MRCH94D9W wrote:

I finally figured it out. It was simply because the directory was not added to Trusted Locations...


Oops, sorry I didn't mention that. It's so common for the last 10+ years that it didn't occur to me.

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