This tip is the fifth in a series of tips that describes 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 the last tip, we found out the best ways to get information on diagrams in an encyclopedia. In this tip we'll create a loop to get information on a collection of diagrams.
If you have closed the work you did in the last tip, reopen it by performing the following steps:
- In System Architect, with the Samples encyclopedia open, select Tools, Macros, VBA Editor to open the VBA IDE.
- 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.
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)
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
Begin a loop by adding the following line:
For i = 1 To myDiagrams.Count
In the Set myDiagram = myDiagrams(16) statement, replace the 16 with the variable i, so that the code reads:
Set myDiagram = myDiagrams(i)
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.
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.
Add the following to the Debug.print command for the diagram:
Debug.print myDiagram.Name & " -- " & myDiagram.TypeName
- 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.
- Click in the gray border to the left of the break we set before, to remove the break circle.
- 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.
In this tip we created a loop to get all diagrams in the encyclopedia, and their type. In the next tip we'll take a look at how we can filter the list of diagrams, so that we get specific ones.
- Continue with the next tip, or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.