Run VBA macro

Run VBA macro

Anonymous
Not applicable
10,312 Views
18 Replies
Message 1 of 19

Run VBA macro

Anonymous
Not applicable

Hello:

 

I apologize because I write very badly in English.

 

I have created a macro in VBA.

 

To execute it I do:

-vbarun (RETURN)

Enter macro name (RETURN)

 

Is there any way to run -vbarun by passing the macro name as a parameter?

This is for when you have to run the macro several times, do not have to type the name of the macro.

 

A greeting

 

0 Likes
Accepted solutions (1)
10,313 Views
18 Replies
Replies (18)
Message 2 of 19

miguelmachadoecosta
Advocate
Advocate
Accepted solution

My suggestion is to place a acaddoc.lsp file inside the instalation directory. In that file you can have multiple commands/shortcuts to run the macros, in the form:

 

(defun c:<command name> ()
  (vl-vbarun "<project path starting in the installation directory>/<name of the project file>.dvb!<name of the module>.<name of the macro>")
  (princ))

 

For example, for command "ABC" to run the macro "hello", which is in "Module3" of project "Project.dvb", which is inside the folder "Macros" that is inside the installation directory:

 

(defun c:ABC ()
  (vl-vbarun "Macros/Project.dvb!Module3.hello")
  (princ))

Message 3 of 19

Anonymous
Not applicable

Hello Miguel:

 

It has worked.

 

Thank you very much.

0 Likes
Message 4 of 19

sieclaprzemyslaw
Contributor
Contributor

Different solution could be to create a button in your palette (if you prefer mouse clicks than keyboard commands).

Just go to your palette -> add new tool -> in "Command string" section put: 

^C^C_-vbarun;"macroName"

1.png

0 Likes
Message 5 of 19

Anonymous
Not applicable

I just came across this thread, and I have a follow-up question for you regarding your method.  This is what I have in my button's command string:  

^C^C_-vbarun;"C:\CentroidFinder.dvb!Module1.Centroidfinder"

But, when I press the button, all I get is AutoCad asking me for a macro:   -VBARUN Macro name:  "C:

 

The macro itself works fine if I run it manually, and if I paste "C:\CentroidFinder.dvb!Module1.Centroidfinder" after the prompt it gives me when pressing the button.  The button just doesn't automatically run the macro.  What am I doing wrong?

0 Likes
Message 6 of 19

Ed__Jobe
Mentor
Mentor

You need to include the project name, e.g. filepath.dvb!ProjectName.ModuleName.MacroName. If you didn't remname it, the default is ACADProject. The VBAMAN dialog shows you what it is.

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 19

Anonymous
Not applicable

Ed,

Thanks for the quick response!  I added the project name.  Now I have this:  

^C^C_-vbarun;"C:\CentroidFinder.dvb!ACADProject.Module1.Centroidfinder"

 

But I'm still getting the exact same issue.  Do I have a syntax problem?  I've checked all the spellings multiple times, so it's not that.  I'm running AutoCAD 2018 if that matters.

 

0 Likes
Message 8 of 19

Ed__Jobe
Mentor
Mentor

Did you verify that ACADProject is the project name? I would take the file off of the root folder and put it in a sub folder that you can add to the Trusted Paths folder list in Options>Files tab. You may have an access issue. For example, I have  C:\AcadCustom that I keep all my code in.

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 9 of 19

Anonymous
Not applicable

I did all of that, still nothing!   As I said, the macro does work when I go to Tools>Macro>Macros>Run.  (the macro name is C:\AcadCustom\CentroidFinder.dvb!Module1.Centroidfinder)  But when I press the button, I get this in the command line:  

 

 _-vbarun Macro name: "C:\

If I fill in the rest of the macro name after the "C:\, it'll run.  Why won't it just do it automatically?

EDIT:  tried to attach, but the site wouldn't let me.  Here's the raw text in Module1:

 

Sub Centroidfinder()
Dim ent(0) As AcadEntity
Dim p1 As AcadLWPolyline
Dim pp As Variant
Dim reg As AcadRegion
Dim center As AcadCircle
Dim varctr As Variant
Dim radius As Double
Dim ctr(2) As Double

ctr(2) = 0
radius = 4

ThisDrawing.Utility.GetEntity ent(0), "Select a polyline: "

If TypeOf ent(0) Is AcadLWPolyline Or TypeOf ent(0) Is AcadPolyline Then
pp = ThisDrawing.ModelSpace.AddRegion(ent)
ent(0).Delete
MsgBox "A region was created"

varctr = pp(0).Centroid
ctr(0) = varctr(0)
ctr(1) = varctr(1)
Set center = ThisDrawing.ModelSpace.AddCircle(ctr, radius)
'ThisDrawing.SendCommand ("MASSPROP" & vbCr)
ElseIf TypeOf ent(0) Is AcadRegion Then
varctr = ent(0).Centroid
ctr(0) = varctr(0)
ctr(1) = varctr(1)
Set center = ThisDrawing.ModelSpace.AddCircle(ctr, radius)
End If

End Sub

0 Likes
Message 10 of 19

Ed__Jobe
Mentor
Mentor

Its not the code, since you can run that using the VBARUN dialog. Its that the command line version can't find it. You didn't say if you verified the project name. See the screenshot of the VBAMAN command.
The yellow area is the Project Name.

 

VBAMAN.png

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 11 of 19

Anonymous
Not applicable

OK, here's a screenshot of everything, maybe you can see something.  I appreciate your patience with me!

image.png

 


image.png

0 Likes
Message 12 of 19

Ed__Jobe
Mentor
Mentor

I don't know. I looks ok. Can you include a screenshot of the upper left corner of the module's code window. It will have the complete path and module name in the title bar.

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 13 of 19

Anonymous
Not applicable

Here you go.  

 

 

image.png

 

0 Likes
Message 14 of 19

Ed__Jobe
Mentor
Mentor

I don't see anything wrong. You might try adding C:\AcadCustom to the Support File Search Path in the Files tab of Options. Did you add it to Trusted Paths?

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 15 of 19

Anonymous
Not applicable

I did both of those..  I guess I'm out of luck for now, but thank you so much for your effort!  If I get lucky and solve it, I'll post the solution here!

0 Likes
Message 16 of 19

Ed__Jobe
Mentor
Mentor

See if it works with lisp. Paste this to the command line.

(vl-vbarun "C:\AcadCustom\CentroidFinder.dvb!ACADProject.Module1.Centroidfinder")

 

If this works, you may want to consider creating a lisp file for your cui. Just name it the same as your cui, but with a *.mnl extension. See this thread I created at augi.com. This way you get the benefit of having commands that you show up in autofill and you can hit Enter to repeat the command.

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 17 of 19

Anonymous
Not applicable

SOLVED!

 

Apparently, it was the backslashes in the name.... When I remove them, this works like a charm:

image.png

0 Likes
Message 18 of 19

Ed__Jobe
Mentor
Mentor

Glad you got it to work. Sorry I didn't catch that. Using the complete path would have worked if you used double backslashes. At the prompt a backslash in a string has special meaning and means that the next character is a code. e.g. "\n" is the new line or {Enter} character. To get the backslash to be a literal character, you need two backslashes. e.g.

(vl-vbarun "C:\\AcadCustom\\CentroidFinder.dvb!ACADProject.Module1.Centroidfinder")

 

Your solution worked because the full path was not needed since you put the dvb in a folder that is in the search path. If you included the full path, it would save a millisecond or two by not having to search for the file.

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

Katsuaki.T
Alumni
Alumni

I found that \ for the project file path does not work in running VBARUN command from Tool palette. Please use / instead to workaround the problem.


Katsuaki Takamizawa
Technical Support Specialist