This is the last tip of this VBA series. By now you've been introduced to System Architect's VBA and learned what you can do with it, you've seen the object model and learned how to use it, and you've built a simple macro that gets information from the repository - in particular you've gotten diagrams in the encyclopedia, symbols on a diagram, and information from the definition of a symbol.
In the last tip, we used the GetPropertyAsCollection method to get Use Case Steps from a Use Case definition. This week, we'll put the final touches on our macro by building a loop around our code to print out the names of all steps of a Use Case. Then we'll wrap the whole thing up.
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:
Creating a loop to get the names of the use case steps
We learned in the last tip that once we used the GetPropertyAsCollection method to get all of the Use Case Steps of a Use Case in one fell swoop, we could use the immediate window's print command (the ?) to print out various properties of items in the collection. For example, we used ? mySteps.Count to get the count of the steps for any Use Case. To get the name of a particular step - for example the second step - we used the ? mySteps.Item(2).Name command.
Obviously, if we wanted to get all the steps of a Use Case, we'd have to 'walk' the collection - set up a loop that would start at 1 and end with the total number of steps in any particular Use Case - which is what mySteps.Count gives us.
So let's try it:
-
In your code, after the statement in which you get the Use Case Steps as a collection,
Set mySteps = mydef.GetPropertyAsCollection("Use Case Steps")
add the following code:
Dim j As Integer For J = 1 to mySteps.Count Debug.Print mySteps.Item(j).Name Next j
Pretty simple stuff - we first tell Visual Basic that this new variable, j, that we are going to use to 'walk' the collection (in other words, it is an index) is an integer. Then for every Use Case Step collection that we encounter, we walk the collection, printing out the name of each 'jth' item - or Use Case Step.
Seems simple, but maybe not. Let's try it.
-
Make sure that all System Architect diagrams are closed (press the Reset button), and step into the code (press the Step Into button).
-
Notice that you immediately get a compilation error - ByRef argument type mismatch:
This is because the Item attribute was expecting a Long (you may have noticed this via intellisense as you were typing it in), but we set our variable 'j' to an Integer. We did this on purpose to make sure you were paying attention. Easily rectified:
-
Change the line ByRef argument type mismatch to read:
Dim j As Long
Now step through the code, and you should see the Use Case Steps for the Use Case Make Reservation printed to the Immediate window.
Finally, let's tidy up our presentation a bit.
-
Change the Debug.Print command to look as follows - this will print the step number before each step:
Debug.Print "Step " & j & ". " & mySteps.Item(j).Name
-
Close any open diagrams, reset the project, and step through the code again. You will get the step number before the steps, as follows:
- Select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.
That's it
Ok, that's it for this 11-part series on learning VBA. Although it may seem like you've learned a lot, you've just touched on the beginning of an exciting world wherein you can manipulate System Architect to do what you'd like, for example:
- Automatically export information directly into other tools, like Microsoft Word, or Access, or Project Manager, or some other third-party tool,
- Automatically import information from a third-party tool into System Architect, so that you can use it in your designs,
- Automatically create diagrams in System Architect from a compilation of other information,
- Modify the behavior of the tool to suit you or your users needs,
- Or simply figure out how to print the Use Case Steps of the macro we created above, to something more interesting than the Immediate window.
To answer these, and any other VBA questions, you can inquire about System Architect's advanced training courses. You can also check back regularly with UNICOM's series of tool tips.