VBA Macro Unload Reference Message

VBA Macro Unload Reference Message

Anonymous
Not applicable
2,008 Views
6 Replies
Message 1 of 7

VBA Macro Unload Reference Message

Anonymous
Not applicable

Got a question for VBA Guru's in ACAD.

We are trying to migrate some old VBA macros to 2018 (then eventually go to .NET)

However several of these have referenced other VBA macros which is not new. The issue has popped up when our automation unloads them. In the past it wasn't a big deal if it tried to unload a macro before its parent was unloaded, no warning or error messages were shown. Now it seems that ACAD 2018 throws a message box stating "Project cannot be closed because it's referenced" I did not think this would be a big deal but it has turned out to be. I thought I could trap it with some error handling and get past the issue. Not the case its not a true error. No error number is thrown.

Any Suggestions???

 

Ray C.

0 Likes
Accepted solutions (1)
2,009 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

If you reference the Microsoft Visual Basic for Applications Extensibility tlb, you can remove the reference and then unload the module. Keep in mind that this will cause an edit to the dvb and it will need to be saved.

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 7

Anonymous
Not applicable

Don't think this would work for our needs. These referenced macros have common methods that are used across many macros. Removing them during unload would cause more issues than I could count. I was hopeful I could trap for this message or just set a setvar that could turn it off. 

Thanks for the suggestion.

 

Ray C.

0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

What do you mean "unload macro before its parent was unloaded", I mean what is the true meaning of "unload" and how your code does it? VBA project can only be unloaded with Visal Basic Extensibility, as @Ed__Jobe pointed out. But you seemed indicating that is not what your automation does. Then what/how "unload" does?

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

Anonymous
Not applicable

Through ACAD menu selections, which calls a Lisp function that autoloads VBA macros. If a macro is already loaded you will also get a message box that says the macro is already loaded. So within the call we unload all macros first before we try and load another. This prevents the load error. Below is a simplified version of our UnloadDVBs Macro.

I am not wanting to unload references within a macro, just unload the macro from ACAD.

 

As stated earlier, when a macro is referenced in another macro, the referenced macro also gets loaded when the parent macro is loaded. The rub comes when unloading all macros the referenced macro may get called to be unloaded prior to the parent, then the message is displayed.

 

 

Public Sub UnloadDVBs()
Dim vbaprojs As Object
Dim VBAProj As Object
Set vbaprojs = ThisDrawing.Application.VBE.vbprojects

For Each VBAProj In vbaprojs
      Application.UnloadDVB (VBAProj.FileName)
Next
End Sub

0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Firstly a slight correct to my previous reply:

 

"VBA project can only be unloaded with Visal Basic Extensibility, as @Ed.Jobe pointed out."

 

Should have been:

 

"Referenced VBA project can only be unloaded with Visal Basic Extensibility, as @Ed.Jobe pointed out."

 

Now I see what you want to do: since in your menu item, you have LISP function to start a VBA Macro, something like:

(Command "VBALOAD" "[path/fileName.dvb]")

(Command "VBARUN" "....")

The problem is the "VBALOAD": if the project has already loaded, AutoCAD pops a message box, which is really annoying and Autodesk should have surpress it, or at least provides a system as option to show or hide it. Just for avoiding this, you have to take all the pains to unload all VBA projects right before calling another VBA macro, whether it is already loaded and readily available or not. Also, to do this for every VBA projects of yours, you need to have that "UnloadDVBs()" available, in order to to the unloading.

 

There is an very simple approach to avoid the annoying "... already loaded" message, which is to use VLisp statement

 

(vl-vbarun "fineName.dvb!modulename.macroname)

 

this VLisp statement will run the macro in specified modle from specified DVB file; more importantly, it would load the DVB file automatically, if it had not been loaded; you can supply a file path, but if not supplied, it would automatically search the drawing's current folder and all support paths. I have been using this to load VBA for years, simple and easy...You need to make sure the DVB files are located in "trusted" folders.

 

Since AutoCAD 2016, there is report on (vl-vbarun ...) stops working in a very subtl way: instead of loading the VBA project and running specified macro, the statement simply returns "T" and does nothing else. This is not easily reproduceable: with exactly the same computer setup, it can happen with one computer, but not the other. It occurred in my office, firstly only on a few computers (Acad2018), and gradually, more. Fortunately, we have stopped using most VBA, and load the 1 or 2 remaining VBA project on AutoCAD startup.

 

But you should still try to use (vl-vbarun...) as your menu item's code to start a macro, to avoid the "... loaded" message popup.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

Anonymous
Not applicable

Great tip with the vl-vbarun method. I will look into using this. 

Thanks for all the suggestions!

Ray C.

0 Likes