We might want to change the wording a bit, where some of it is confusing, but overall, a good article. Great job DB
Registered forum members do not see this ad.
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
PS. there will be some formatting and pictures going in here so don't worry too much about layout and stuff.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 <enter>
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.
Life doesn't suck, although we all go through periods when it may be easier to think that, than to discern the solution to whatever problem is the most formidable
at the moment in one's personal UCS.
Go to PLAN view instead. - Dadgad
We might want to change the wording a bit, where some of it is confusing, but overall, a good article. Great job DB
Everyone has a Photographic memory, some just don't have film
if you think that is confusing you should have seen what I gave Tiger to work with.![]()
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
now thats funny. Been there, feel your pain
Everyone has a Photographic memory, some just don't have film
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.
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?
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
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 !
Life's constantly changing - keep up or get left behind
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.
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
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.”
Registered forum members do not see this ad.
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.
"That's it. It's one thing for a ghost to terrorize my children, but quite another for him to play my Theremin." Homer Simpson
Dave
Bookmarks