VBA and DVB newbie question

VBA and DVB newbie question

h_s_walker
Mentor Mentor
2,467 Views
5 Replies
Message 1 of 6

VBA and DVB newbie question

h_s_walker
Mentor
Mentor

Ok I'm not a VB newbie not by a longshot, but VBA and Autocad. TOTAL NEWBIE.

 

 

Now I've converted a VB program of mine to vba and running it from VBA Manager works as expected.

 

1. A dialog box pops up.

2. The user enters a load of numbers.

3. A polyline is drawn.

4. Dialog box closes.

 

Now how can I add that dvb to a button so that I can click on it and have it run.

 

 

ALSO

 

I would like to check if a layer exists in the drawing. If the layer doesn't exist create it, otherwise continue with the rest of the program.

 

The layer by the way is called REINFORCEMENT

Howard Walker
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.

EESignature


Left Handed and Proud

0 Likes
Accepted solutions (2)
2,468 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor
Accepted solution

To run VBA macro from menu/toolbar/ribbon item is not much difference to run a LISP command: you can simply write an AutoCAD scrip macro to record your command line action and assign it to your menu/toolbar item. But, you need to make sure the *.dvb is loaded, which you can load on AutoCAD startup by using acad.dvb, or acad.lsp...

 

However, a much simple way would be using lisp statement (vl-vbarun...), which load a *.dvb, if not yet loaded, into AutoCAD and runs a VBA macro defined in that *.dbv.

 

Assume you know how to customize menu/toolbar/ribbon with CUI command: define a command, and assign this macro to the command:

 

^C^C^C(vl-vbarun "myApp.dvb!myModule.MyRoutine")

 

Then you can have menu item/toolbarbutton/ribbon item this command.

 

Notice that the you can also include full file path for the *.dvb name. If not path, AutoCAD will search support paths for the *.dvb file to load.

 

As for check whether a layer exists, it is fairly easy in AutoCAD VBA, pseudo-code:

 

Dim lay as AcadLayer

Dim exists As Boolean

For Each lay in ThisDrawing.Layers

  If UCase(lay.Name)=UCase(targetLayerName)

    exists=True

    Exit For

  End If

Next

 

If Not exists Then

  ''Create new layer here

End If

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

h_s_walker
Mentor
Mentor

Here's the DVB file attached.

 

You'll notice that in no part of it does it say "Show this form" However at the end it does say "Hide the form".

 

It will run from VBA as expected. However I want to run it from a button

Howard Walker
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.

EESignature


Left Handed and Proud

0 Likes
Message 4 of 6

h_s_walker
Mentor
Mentor
Accepted solution

Stupid me forgot to add a module for my macros

 

Program now works as expected

Howard Walker
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.

EESignature


Left Handed and Proud

0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor

Your VBA project is "not completed", because it lacks a VBA macro (VBA command), so you can "run" the form inside VBA editor, but no way to run it outside VBA editor as user.

 

The very basic way to run VBA code for user (who knows nothing about VBA/VBA Editor) is to enter command "VBARUN" (or ALT+F8), which bring up VBA Macros winodw, which lists all available VBA macros. In your VBA Project, this window is empty because there is no VBA macro your VBA code. You need to create one, like this:

 

1. in VBA editor, add a module, give it a proper name, if preferred, but it has default name, like "Module1";

2. Add following code to start the form:

 

Option Explicit

Public Sub ShowMyForm()
    WIBeam.show
End Sub

 

Now, if you enter command "VBARUN", you will see a VBA macro available for user to run. 

 

Now you can incorperate this VBA macro with the menu/toolbar macro as I explained in previous reply.

 

HTH.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 6

h_s_walker
Mentor
Mentor

Already done.

 

You don't need Option Explicit if it's just showing a form

Howard Walker
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.

EESignature


Left Handed and Proud

0 Likes