Using VBA Sample Code (for the Non-Programmer)

You don’t know anything about VBA but you’ve found some interesting VBA code somewhere on the web, in a newsgroup, or maybe even here.  So now what do you do with that code to be able to use it in Inventor?  Here are the cookbook steps:

  1. From within Inventor, press Alt-F11 or run the VBA Editor command from the Tools tab of the ribbon.  This will open the VBA development environment, as seen below. VBAEditor

  2. The Project Explorer is the window containing the tree at the upper-left in the picture. 
    • Expand the node labeled “ApplicationProject”. 
    • Expand the node labeled “Modules”
    • Double-click on the node labeled “Module1”
      An editor window will pop-up, similar to the picture below. (In the picture below I have one document open so there are two top-level nodes in the project explorer.  One node is for the application project and the other node is for the document.  Make sure you use the application project.)

    VBAEditorModule1

  3. Go to wherever the original VBA code is (browser, new reader, email, etc.) and copy it to the clipboard.
  4. Click within the VBA code window and paste the code.  You should see something similar to the picture below.

    VBAEditorCode

  5. Scan through the code to see if there are any problems.  Do this by scrolling down through the pasted code looking for any red lines.  If you don’t see any problems go to step 6, otherwise continue with this step.

    The most common issue when pasting code is that lines that should be a single line may end up wrapping onto two lines.  VBA expects a statement to be on a single line.  The example below illustrates the problem where what should be a single line has been broken and wraps onto the next line.

    VBABadCodeSample

    Fix it by editing it so it is on a single line.

    There’s also an exception to the statement I made above about the requirement of a statement only being a single line.  It’s possible to break a statement into multiple lines by adding a “ _” (space and underscore) at the end of a line.  This is a line continuation character and indicates that the statement continues on the next line.  That’s shown in the top statement in the previous picture.  You may need to understand that to better understand why a line is shown in red.

    If you see other problems, it beyond the scope of this post to diagnose those.  It would be best to go back to the author or to ask someone that has some familiarity with the API.

  6. Close the VBA window.
  7. Run the program by pressing Alt-F8 or running the Macros command from the Tools tab.  This will display the Macros dialog, as shown below.

    MacrosEmptyDialog

  8. In the “Macros in” drop-down list select “Application Project” and choose the macro you want to run from the list, as shown below.

    MacrosDialog

  9. Click the “Run” button.

For some other ways to execute a macro see the post Creating Buttons for VBA Macros.


Comments

4 responses to “Using VBA Sample Code (for the Non-Programmer)”

  1. marcos Avatar
    marcos

    Iam a old mechanical engineer who need to learn more about VBA this is good start for me keep doing it
    Thanks
    Marcos Diaz
    Dominican Republic

  2. Thanks.
    It’s really good..

  3. I have used the code bellow ( Autolisp code ) into AutoCad 2004 to make baloons.
    Now we migrated for Autodesk Inventor 2011 but we get the following error:
    ** Esse comando não pode ser chamado de modo transparente **
    ; erro: Função cancelada
    Does anybody know if the new version of the Autocad ( Inventor 2011 ) doesn´t work correctly with Autolisp routines? Or could somebody give qa help to solve this error?
    Variaveis:
    blndia balao diameter, GLOBAL
    blnnum balao number, GLOBAL
    cen Center of balao
    ep Endpoint of arrow
    oom Old osnap mode
    sp Start point of arrow
    temp Temp. value for blndia and blnnum input
    ts Name of current text style
    tshei Text style height |;
    ;10====PROGRAM SETUP
    (defun c:balao (/ cen ep oom sp temp ts tshei)
    (setq oom (getvar “osmode”))
    (graphscr)
    ;20====GET SIZE, START POINT, CENTER, & NUMBER
    (if (null blndia)
    (if (= 0 (getvar “dimscale”))
    (setq blndia (/ (getvar “dimtxt”) 0.28))
    (setq blndia (/ (* (getvar “dimtxt”) (getvar “dimscale”)) 0.28))
    )
    )
    (initget 6)
    (setq temp (getreal (strcat “\nDiametro do Balão : “)))
    (if temp (setq blndia temp))
    (setvar “osmode” 512)
    (initget 1)
    (setq sp (getpoint “\nIndique a cota: “))
    (setvar “osmode” 0)
    (initget 1)
    (setq cen (getpoint sp “\nLocal do balao: “))
    (if (null blnnum) (setq blnnum 0))
    (setq blnnum (1+ blnnum))
    (initget 6)
    (setq temp (getint (strcat “\nNúmero do Balão : “)))
    (if temp (setq blnnum temp))
    ;30====CALCULATE END POINT OF ARROW, GET TEXT STYLE HEIGHT, DRAW balao
    (setq ep (polar cen (angle cen sp) (/ blndia 2)))
    (setq ts (getvar “textstyle”))
    (setq tshei (cdr (assoc 40 (tblsearch “style” ts))))
    (if (> tshei 0) (princ “\nEstilo do texto tem altura fixa; pode não caber no balão.”))
    (setvar “cmdecho” 0)
    (command “.dim” “LEADER” sp ep) (command)
    (command “.circle” cen “D” blndia)
    (if (> tshei 0)
    (command “_.text” “M” cen 0 blnnum)
    (command “.text” “M” cen (* 0.28 blndia) 0 blnnum)
    )
    ;40====RESET
    (setvar “cmdecho” 1)
    (setvar “osmode” oom)
    (princ)
    )

  4. Hi, I’m new in development of macro for inventor but the code used in your screen seem to be very interesting for me (Flattern assembly) It’s exactly what I wanna do to be more iefficient :)
    Is it possible to get it ? I’m also looking for a code to save all flat patterns of sheemetal parts in a assembly which contains standard parts and sheetmetal parts, any suggestion to modify the code ? MAny thanks for your help.

Leave a Reply to marcosCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading