Removing the module using code!

Removing the module using code!

abbas.baghernezhad
Enthusiast Enthusiast
845 Views
5 Replies
Message 1 of 6

Removing the module using code!

abbas.baghernezhad
Enthusiast
Enthusiast

I guess it's nice to remove the module from the file right after the job is done. I found these lines for Excel vba. of course it doesn't work in Autocad. 

 

Application.DisplayAlerts = False
ActiveWorkbook.VBProject.VBComponents.Remove ActiveWorkbook.VBProject.VBComponents("Module1")

 

 So I tried many different efforts to make this work. I'm  Still searching... for example this didn't work:

 

ThisDrawing.Application.VBComponents.Remove "Module1"

 

How can I remove my module at end of my code?

0 Likes
Accepted solutions (1)
846 Views
5 Replies
Replies (5)
Message 2 of 6

Ed__Jobe
Mentor
Mentor

Why are you trying to do that?

First, it only works if your vba is embedded in the dwg, not if you store your code in a dvb. Second, it doesn't unload the code from memory, it just deletes the module from your project, requiring you to save your project and thus you will lose your code for use the next time.

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

abbas.baghernezhad
Enthusiast
Enthusiast

Yes , I know what it does. I send my files to customers and they asking why my Dwgs have some macro in it. Extra clicks bothers them and they think it is a malware. So I have to remove the macro everytime.

 

Is there another way which the macro wouldn't be embedded in the file?

0 Likes
Message 4 of 6

Ed__Jobe
Mentor
Mentor
Accepted solution

Yes, I never put code in a dwg. Run VBAMAN and you will see at the top of the dialog, the embedded project will be listed. Click on the Extract button and save it to a *.dvb file. Make sure that the folder you save it to is listed in the Options>File>Support File Search Path and Options>Files>Trusted Folders.

 

Now you just need to load the dvb when you want to use the commands you created. For me, the best way is to create a cuix with the toolbars or ribbon commands you will use. Then create a *.mnl file with the same name as your cuix. The mnl contains lisp. You can use the following lisp to load your dvb when the menu loads.

 

;;; Ed's toolbox
;;; support functions for EngineeringUtilities.cuix routines
;;;________________________________________

(setq supdir "c:\\AcadCustom\\support_GS\\")

;;; *********************
;;; setq global variables
;;; *********************
(vl-load-com)
;;; get the application object
(setq *acadObj* (vlax-get-acad-object))

;;; *********************
;;; load vba modules used by engr.mnu
;;; *********************
(vl-vbaload (strcat supdir "Toolbox.dvb"))

;;    array of command name strings your dvb has.
;;    i.e. the names of all the Public Sub methods.
(setq CommandList
       (list
	 "EraseClouds"	
	 "PrintSchedule"
	 "SaveAll"	
	 "SetViewportScale"	
	)
)

;;    make a defun for each command name
;;    commands  that will run in the document context
;;    This way you can run a command without the VBARUN command.
;;    It makes it easy to repeat the last command too, [Enter].
(foreach CommandName CommandList
  (eval
    (list 'defun
	  (read (strcat "C:" CommandName))
	  '()
	  (list 'vla-RunMacro *AcadObj* CommandName)
	  '(princ)
    )
  )
)

;;; *********************
;;; end loading engr.mnl
;;; *********************
(princ "\nEngineering Utilities Menu loaded")

 

 Save the above lisp as yourcuixname.mnl. Change the path and filenames to match your setup. For further discussion, look 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 5 of 6

abbas.baghernezhad
Enthusiast
Enthusiast

It is wonderful... and complicated.

- I added the dvb folder to the lists.

- Changed the lisp to Mymacro.mnl with changing:

(setq supdir "D:\\Programing\\VBA\\AutoCAD")

(vl-vbaload (strcat supdir "Mymacro.dvb"))

- in CUI , I created a command named Mymacro with

Name: Mymacro

what else?

0 Likes
Message 6 of 6

Ed__Jobe
Mentor
Mentor

If you create a ribbon panel in a new cuix, and name the cuix "MyMacro.cuix", and name the lisp MyMacro.mnl, Then when you load the cuix, the MyMacro.mnl will automatically load and then it loads the dvb used by your ribbon buttons.

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