Creating a VBA Macro -- Part 5
This week's tip is the fifth in a series of tips that will describe System Architect's VBA and show you how to build a simple macro that gets information from the repository. In this series, you create a macro, use System Architect's object model to access an encyclopedia, access diagrams in the encyclopedia, get symbols on a diagram, and get information from the definition of a symbol.
In last week's tip , we found out the best ways to get information on diagrams in an encyclopedia. This week we'll create a loop to get information on a collection of diagrams.
If you have closed the work you did in last week's tip, reopen it by performing the following steps:
1. In System Architect, with the Samples encyclopedia open, select Tools, Macros, VBA Editor to open the VBA IDE.
2. Within the IDE, open the Example module that we created in last week's tip. By taking the previous tips to this point, your code should look as follows:
Getting All the Diagrams
Last week we used the Diagram object's count and item attributes to get specific diagrams in an encyclopedia (ie, the 10th one created). Normally we won’t know how many diagrams there are in an encyclopedia. Let’s set up a loop to walk all the diagrams, and print out their name. We will replace the number 16.
1. Add a line of space in the code, by hitting your Enter key between the lines:
Dim MyDiagram As SA2001.Diagram
Set myDiagram = myDiagrams(16)
2. At the end of the statement, Dim MyDiagram As SA2001.Diagram , add the following code -- , i as Integer -- so that the line reads as follows:
Dim MyDiagram As SA2001.Diagram , i as Integer
3. Begin a loop by adding the following line:
For i = 1 To myDiagrams.Count
4. In the Set myDiagram = myDiagrams(16) statement, replace the 16 with the variable i , so that the code reads:
Set myDiagram = myDiagrams(i)
5. At the end of the Debug.print myDiagram.Name statement, add a ‘Next i’ statement.
Your final code should look like this:
Stepping Through the Code
Now let’s step through the code.
6. Click the Reset button to reset the project, then click the Step Into button repeatedly. Notice how, as you walk through the loop we have created, every time you step through the Debug.Print command, a new diagram name is printed to the Immediate window.
Let’s also print out the diagram’s type along with the name, getting it using the same myDiagram object.
7. Add the following to the Debug.print command for the diagram:
Debug.print myDiagram.Name & “--“& myDiagram.TypeName
8. Step through the code again. Notice that each diagram’s name and type is printed to the Immediate window.
Run the Program
Let’s now run our program in full.
9. Click in the gray border to the left of the break we set before, to remove the break circle.
10. Select Run, Run Sub/UserForm , or alternatively, click on the Run button on the menu. The program will run, and print out all of the diagram names and their types in the currently open encyclopedia.
Next Week's Tip
This week we created a loop to get all diagrams in the encyclopedia, and their type. Next week we'll take a look at how we can filter the list of diagrams, so that we get specific ones.
11. Continue with next week's tip , or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001 .