Run a vba macro

Run a vba macro

m09366023695
Enthusiast Enthusiast
1,839 Views
13 Replies
Message 1 of 14

Run a vba macro

m09366023695
Enthusiast
Enthusiast

I write a example vba macro like this:

 

Public Sub main()
   MsgBox "hi"
End Sub

 

I stored it at :

 

F:/gr/masoud.dvb

 

I write a lsp file like this masoud.lsp:

 

(defun s::startup()
  (command "-vbaload" "F:/gr/masoud.dvb")
  (setvar "cmdecho" 0)
  (defun c:MAINGR() 
  (command "vbarun" "masoud.main")
)

 

I load this macro at autocad:

1. run CUI command

2. LISP Files

3. load masoud.lsp

 

but when I run this command: 

MAINGR 

it says:

Unknown command MAINGR

 

moderator: added code to code windows

0 Likes
Accepted solutions (1)
1,840 Views
13 Replies
Replies (13)
Message 2 of 14

cadffm
Consultant
Consultant

Hi,

 

>>defun s::startup

is for every document (dwg file), but you don't need to load a dvd in every document, just one time in a program session.

AND your lisp loads with every document to, that's doubled (and you don't need the vba-load)

Perhaps I am confused - or your structure is (I will re-read it in 5minutes again).

 

Untested, just an idea:

Are the folders of your lsp and dvd   trusted paths?

Options/files/ trusted location

https://help.autodesk.com/view/ACD/2023/ENU/?guid=GUID-A90CDF15-CF58-46AC-82BB-D437F6BBCA89

Sebastian

0 Likes
Message 3 of 14

Ed__Jobe
Mentor
Mentor

First, there are several syntax errors in your lisp.

Second, as @cadffm  mentioned, s::Startup has special uses and I don't recommend it to be used for loading vba. It should be self-contained in your cui. Just put your lisp command defining code in an *.mnl file named the same as your cui. When the cui is loaded, the mnl will be loaded at the same time. Here is a tip I wrote that will help you with your lisp code. In your cuix, create a command with a macro that runs the lisp defun created in your mnl. For example, "^C^Cmaingr "

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

Message 4 of 14

seabrahenrique
Advocate
Advocate

Nice tip @Ed__Jobe.

 

I use in that way @m09366023695 :

 

(defun c:MyComand ()
(VL-VBALOAD "C:/Path/file.dvb")
(vl-vbarun "MyComand")
)

 

And then i put the command "MyComand" in a button of CUI.

 

I prefer to load my codes when user click in one button of my application, instead to load always with initialization of AutoCAD (sometimes the user don't need our plug-in).

 

Just a suggestion to add a discussion.

0 Likes
Message 5 of 14

MakCADD
Advocate
Advocate

no need to load

Vbarun is enough.

Try path&filename.dvb!modulename.procedure

Message 6 of 14

Ed__Jobe
Mentor
Mentor

@MakCADD wrote:

no need to load

Vbarun is enough.

Try path&filename.dvb!modulename.procedure


@MakCADD is correct. Although, in some cases as I noted in my post at augi, sometimes its preferable to use (command "vbarun") rather than (vl-vbarun).

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 14

m09366023695
Enthusiast
Enthusiast

can u show me a correct lsp file? I confused. 

0 Likes
Message 8 of 14

cadffm
Consultant
Consultant

Create a lsp file, content:

 

(defun c:MAINGR ()

   (setvar "cmdecho" 0)
   (command "vbarun" "F:/gr/masoud.dvb/masoud.main") 

(princ)

)

 

Restart Acad, open a file  use APPLOAD and load this file.

Is it working?

 

If yes: add this .lsp to your cuix file (in CUI Dialog).

 

it should work now as you like.

 

 

Sebastian

0 Likes
Message 9 of 14

m09366023695
Enthusiast
Enthusiast

If I have a userForm, this solution works fine?

0 Likes
Message 10 of 14

MakCADD
Advocate
Advocate

In ur code edit 

 (defun c:MAINGR() 

  (command "vbarun" "F:/gr/masoud.dvb!masoud.main"))

 

 

 

 

Message 11 of 14

cadffm
Consultant
Consultant
Accepted solution

I had a typo, but MakCADD corrected.

 

Why not (except my typo)

 

Try it in your commandline,

Start AutoCAD, type in:

(command "vbarun" "F:/gr/masoud.dvb!masoud.main")<enter>

 

Works?

Make sure "F:/gr" and where your .lsp, is a trusted location in your Options/Files/

 

If it works:

The whole content of your .lsp file:

(defun c:MAINGR () 

  (setvar "cmdecho" 0")

  (command "vbarun" "F:/gr/masoud.dvb!masoud.main")

 (princ)

)

 

Add this file as LISP to your cuix in CUI dialog, restart Acad, type in 

MAINGR<enter>

Works? 

 

 

Sebastian

0 Likes
Message 12 of 14

cadffm
Consultant
Consultant

And?

Sebastian

0 Likes
Message 13 of 14

Retob86
Explorer
Explorer

I have a lot of custom Userforms.

Usually the main VBA Function only contains the loading of the userform with command "userform_Name.show"

 

How I do this to start Userform by Button in UI (without LISP):

 

1 .I load the dvb file with <Manage> <Load applications> <Contents> button (Startup Suite)

 

2. Then I can create a new Button to User Interface by adding a custom command.

 

3. There I can add the macro name: "^C^C_vbarun;Module1.mymacro"

 

--> ^C^C_vbarun;<Modulename>.<macro_name>"

 

I hope this will help.

 

0 Likes
Message 14 of 14

Ed__Jobe
Mentor
Mentor

It really is best to create a command name using lisp to execute your vba rather than using vbarn in your button macro. It's not hard and it doesn't take long. If you add a vba command, it's easy to update.

 

If you use the full path to your macro in the vbarun command, the dvb will be loaded. No need to reference it in appload.

 

If you create a command, not only can you type it at the command line ( and even assign a shortcut to it), but it allows you to hit {Enter} to repeat the command and it stays in the command history. The tip I linked to in post 3 gives you everything you need to know.

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