Creating a VBA Macro -- Part 11
Creating a VBA Macro -- Part 11
Ok, we lied. This was supposed to be a 10-part tip series, but we made it 11. So this week is definitively the last tip of this particular 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 last week's 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 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:
Creating a Loop to Get the Names of the Use Case Steps
We learned last week 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:
1. 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
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.
2. Make sure that all System Architect diagrams are closed (press the Reset button), and step into the code (press the Step Into button).
3. 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:
4. Change the line ByRef argument type mismatch to read:
Dim j As Long
5. 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.
6. 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
7. Close any open diagrams, reset the project, and step through the code again. You will get the step number before the steps, as follows:
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 (not to mention improve your resume by adding VBA to it), 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.
You may further your knowledge by taking the 7-part series on VBA with System Architect -- Creating System Architect Diagrams, Symbols, and Definitions in VBA
More support for:
Rational System Architect
Software version: 11.0, 11.1, 11.2, 11.3, 11.3.1, 11.4
Operating system(s): Windows
Reference #: 1325910
Modified date: 20 March 2009