+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Super Moderator Tiger's Avatar
    Computer Details
    Tiger's Computer Details
    Operating System:
    Windows 7 Enterprise 64 bit
    Computer:
    Dell Precision M4500
    CPU:
    Intel Core i5 2.40GHz
    RAM:
    8GB
    Graphics:
    NVIDIA Quadro FX 880M
    Primary Storage:
    280 GB
    Monitor:
    2 x Samsung SyncMaster 2443 24''
    Using
    AutoCAD 2012
    Join Date
    Nov 2006
    Location
    Sthlm, Sweden
    Posts
    4,595

    Default FAQ - How do I run a VBA routine?

    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

    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.
    PS. there will be some formatting and pictures going in here so don't worry too much about layout and stuff.
    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

  2. #2
    Senior Member CmdrDuh's Avatar
    Computer Details
    CmdrDuh's Computer Details
    Computer:
    HP workstation xw8200
    RAM:
    2gig, soon to be 4
    Monitor:
    Dual 21s
    Using
    AutoCAD 2009
    Join Date
    May 2008
    Location
    AZ, USA
    Posts
    396

    Default

    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

  3. #3
    Luminous Being dbroada's Avatar
    Computer Details
    dbroada's Computer Details
    Operating System:
    XP Pro
    Computer:
    Dell
    CPU:
    Intel Xeon 2.13GHz
    RAM:
    2GB
    Graphics:
    NVIDA Quadro FX 580
    Monitor:
    DELL 23" & SAMSUNG 21"
    Discipline
    Electro/Mech
    dbroada's Discipline Details
    Occupation
    Design Engineer
    Discipline
    Electro/Mech
    Using
    Electrical 2013
    Join Date
    Nov 2005
    Location
    Sussex, UK
    Posts
    5,024

    Default

    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

  4. #4
    Senior Member CmdrDuh's Avatar
    Computer Details
    CmdrDuh's Computer Details
    Computer:
    HP workstation xw8200
    RAM:
    2gig, soon to be 4
    Monitor:
    Dual 21s
    Using
    AutoCAD 2009
    Join Date
    May 2008
    Location
    AZ, USA
    Posts
    396

    Default

    now thats funny. Been there, feel your pain
    Everyone has a Photographic memory, some just don't have film

  5. #5
    Super Member SEANT's Avatar
    Using
    AutoCAD 2012
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    1,968

    Default

    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.

  6. #6
    Luminous Being dbroada's Avatar
    Computer Details
    dbroada's Computer Details
    Operating System:
    XP Pro
    Computer:
    Dell
    CPU:
    Intel Xeon 2.13GHz
    RAM:
    2GB
    Graphics:
    NVIDA Quadro FX 580
    Monitor:
    DELL 23" & SAMSUNG 21"
    Discipline
    Electro/Mech
    dbroada's Discipline Details
    Occupation
    Design Engineer
    Discipline
    Electro/Mech
    Using
    Electrical 2013
    Join Date
    Nov 2005
    Location
    Sussex, UK
    Posts
    5,024

    Default

    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

  7. #7
    Forum Deity NBC's Avatar
    Using
    AutoCAD 2009
    Join Date
    Aug 2007
    Location
    Manchester, UK
    Posts
    2,109

    Default

    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

  8. #8
    Luminous Being dbroada's Avatar
    Computer Details
    dbroada's Computer Details
    Operating System:
    XP Pro
    Computer:
    Dell
    CPU:
    Intel Xeon 2.13GHz
    RAM:
    2GB
    Graphics:
    NVIDA Quadro FX 580
    Monitor:
    DELL 23" & SAMSUNG 21"
    Discipline
    Electro/Mech
    dbroada's Discipline Details
    Occupation
    Design Engineer
    Discipline
    Electro/Mech
    Using
    Electrical 2013
    Join Date
    Nov 2005
    Location
    Sussex, UK
    Posts
    5,024

    Default

    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

  9. #9
    Super Member SEANT's Avatar
    Using
    AutoCAD 2012
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    1,968

    Default

    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.”

  10. #10
    Luminous Being dbroada's Avatar
    Computer Details
    dbroada's Computer Details
    Operating System:
    XP Pro
    Computer:
    Dell
    CPU:
    Intel Xeon 2.13GHz
    RAM:
    2GB
    Graphics:
    NVIDA Quadro FX 580
    Monitor:
    DELL 23" & SAMSUNG 21"
    Discipline
    Electro/Mech
    dbroada's Discipline Details
    Occupation
    Design Engineer
    Discipline
    Electro/Mech
    Using
    Electrical 2013
    Join Date
    Nov 2005
    Location
    Sussex, UK
    Posts
    5,024

    Default

    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

Similar Threads

  1. Looking for a Lisp routine
    By StykFacE in forum AutoLISP, Visual LISP & DCL
    Replies: 37
    Last Post: 18th Jul 2012, 07:46 pm
  2. Lisp Routine help
    By pryzmm in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 11th May 2008, 03:50 pm
  3. Just another routine, pls take a look
    By pryzmm in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 30th Jul 2007, 04:04 pm
  4. Printing Routine Help !!!
    By pryzmm in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 27th Jul 2007, 04:45 am
  5. lisp routine
    By iain9876 in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 13th Oct 2006, 04:39 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts