Jump to content

FAQ - How do I run a VBA routine?


Tiger

Recommended Posts

Hey guys!

 

Our very own DB has written up a how-to-guide on How to load VBA - we could use a couple more eyes on this though so that we're not missing anything - so for all the VBA whizzes out there, read and comment and I'll be in your debt :)

 

How do I load a VBA routine?

 

VBA stands for Visual BASIC for Applications and is a Microsoft package available for many applications such as Excel. Most MS Office packages come with VBA and they are loosely based on Visual BASIC and while broadly similar are not identical.

 

A VBA routine can look something like this, this is a routine to scale blocks drawn with metric units to imperial units.

 

Public Sub ScaleRef()

Dim myObject As AcadEntity

Dim myScale As Double

myScale = 1 / 25.4

ThisDrawing.Utility.GetEntity myObject, basePnt, "Select an object"

If myObject.ObjectName = "AcDbBlockReference*" Then

basePnt = myObject.InsertionPoint

End If

 

myObject.XScaleFactor = myScale

myObject.YScaleFactor = myScale

myObject.ZScaleFactor = myScale

End Sub

 

To load the routine, first start up AutoCAD.

Select Tools > Macro > Visual Basic Editor

 

This will bring up the Visual Basic Editor screen. All routines are built here but if no routines are loaded it should be quite plain.

 

Select Insert > Module and a blank code window appears, either write a new VBA routine or paste an already written routine here.

 

Note the first line, in this code it is Public Sub ScaleRef. This means that ScaleRef is the name that is used to activate the routine.

 

In the Visual Basic editor window to the left is a project explorer window - if multiple routines are going to be used it is recommended to change the names of the routines from the default Module1 to something more descriptive. This is done by entering a new name in the Properties box, underneath the project explorer window.

 

Select File > Close and return to AutoCAD.

 

Once back in AutoCAD the routine is ready to be run, this is done in two steps, first a command (vbastmt) to active VBA and then the particular routine is activated.

 

For this routine, type the following:

 

vbastmt

at the prompt Expression: type ScaleRef

 

Now the prompt Select an object: appears and when a block is selected it is automatically scaled from metric to imperial.

 

When exiting AutoCAD, a dialogue box pops up asking if changes to the VBA project "Global1" should be saved, click the Yes-button. It should be saved as ACAD.dvb and saved in the Support folder. Doing this means that the routine will be loaded each time AutoCAD starts. If it is saved under another name and/or in another location, it will need to be loaded each time using the VBA Manager.

 

The VBA Manager is found under Tools > Macro. Click Load and browse to the location of the file.

 

PS. there will be some formatting and pictures going in here so don't worry too much about layout and stuff.

Link to comment
Share on other sites

Great idea. I imagine most of us who use VBA regularly forget how inaccessible it may seem to the uninitiated. This will broaden VBA’s acceptance in the AutoCAD user community.

 

 

Two minor notes regarding the FAQ:

 

Depending on the setting Visual Basic Editor – Tools – Options – Editor – Require Variable Declaration (Option Explicit header), the sample routine may generate an error at the variable “basePnt”. Including a line “Dim basePnt as Variant” at the top would avoid problems regardless of setting.

 

Another way to have a vba project (.DVB file) available without having to load it manually is to place it in AutoCAD – Tools – Load Application – Startup Suite.

Link to comment
Share on other sites

thanks Seant.

 

I have require variables set so am not sure why I missed that. I must have turned it off at some time.

 

I wasn't sure of how to load individual files so thanks for that bit too. I tend to add more bits to my acad.dvb and thought there must be another way but never got around to looking.

 

Any more comments anybody?

Link to comment
Share on other sites

Am I missing something here ?

If the FAQ is entitled 'How do I run a VBA routine?' then do we really need to see the actual VBA code itself ?

 

Surely the FAQ should be more about how to load/run VBA routines; than how to actually create a VBA routine ?

Isn't that how the LSP one is written ?

Just my 2p !

Link to comment
Share on other sites

the LISP one also includes a sample code. It is to give an idea of what a VBA routine looks like. SEANT's post pointed out that some machines may already have the declare all variables set and the included routine wouldn't work.

Link to comment
Share on other sites

Would the eventual goal be to create a Resources sub-forum, similar to “AutoLISP Archive”, to host VBA routines? If so, I have some that I’d be willing to submit for inclusion.

 

I guess this process may repeat itself as the .NET languages become more popular. Currently, however, it may be prudent to treat that as a matter of “in due time.”

Link to comment
Share on other sites

there may well be place for a resource but this came up when somebody posted a solution to a problem using VBA rather than LISP. We have an FAQ on how to run LISP but I didn't think we had one on how to run VBA.

 

I'm OK at writing my own routines and handing them round the office but I wasn't comfortable on instructing others on how to use them. I wrote some words that Tiger has put into English and she put it here for other eyes to view and comment.

Link to comment
Share on other sites

there may well be place for a resource but this came up when somebody posted a solution to a problem using VBA rather than LISP.

that was probably my fault? I tend to do that, as I try to answer people without forcing a language choice. Or sometimes, I just don't know that they wanted lisp. :)

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...