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:
- 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.
- 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.)
- Go to wherever the original VBA code is (browser, new reader, email, etc.) and copy it to the clipboard.
- Click within the VBA code window and paste the code. You should see something similar to the picture below.
- 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.
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.
- Close the VBA window.
- 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.
- In the “Macros in” drop-down list select “Application Project” and choose the macro you want to run from the list, as shown below.
- Click the “Run” button.
For some other ways to execute a macro see the post Creating Buttons for VBA Macros.