Tiger Posted December 19, 2008 Posted December 19, 2008 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. Quote
CmdrDuh Posted December 19, 2008 Posted December 19, 2008 We might want to change the wording a bit, where some of it is confusing, but overall, a good article. Great job DB Quote
dbroada Posted December 19, 2008 Posted December 19, 2008 if you think that is confusing you should have seen what I gave Tiger to work with. Quote
CmdrDuh Posted December 19, 2008 Posted December 19, 2008 now thats funny. Been there, feel your pain Quote
SEANT Posted December 19, 2008 Posted December 19, 2008 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. Quote
dbroada Posted December 19, 2008 Posted December 19, 2008 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? Quote
NBC Posted December 20, 2008 Posted December 20, 2008 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 ! Quote
dbroada Posted December 20, 2008 Posted December 20, 2008 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. Quote
SEANT Posted December 20, 2008 Posted December 20, 2008 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.” Quote
dbroada Posted December 20, 2008 Posted December 20, 2008 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. Quote
CmdrDuh Posted December 22, 2008 Posted December 22, 2008 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. Quote
nafta Posted October 10, 2010 Posted October 10, 2010 Thanks a lot for this. I ran my first vba routine to draw a parabola in CAD in order to represent a railroad vertical curve. Quote
Recommended Posts
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.