This tip is the third of a series of tips that describe System Architect's VBA and show you how to build a simple macro that gets information from the repository. In the 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 created a macro project, module, and subroutine, and instantiated the Application object. In this tip we will get information about the active encyclopedia. Before we do that, however, we'll take a quick look at some of the tools available to us in VBA's environment.
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. Your code should look as follows:
Using tools of the VBA environment
VBA provides three convenient tools that enable you to check your code as you write it, instead of having to compile and run it to see if you've done something wrong. Let's take a look at these tools before we continue coding our macro.
Setting a breakpoint
- On the grey margin, next to the line of code Set SAapp = Nothing, click once inside the column. A red bullet will appear. You have set a breakpoint - it will stop the code before it executes that command. What it means is that SAapp is active at the breakpoint.
Using the step into command
- Right-mouse click in the toolbar, and select Debug. The Debug toolbar will appear. You can dock the Debug toolbar to the main menu frame.
- Notice that the Debug menu gives you a Step Into Command.
- Place your cursor anywhere in the top line of the code you have written so far. Click on the Step Into command button. Click it a second time. Notice that each time you click the command, a new line of code becomes highlighted, as you step through the code.
- With the following line highlighted: Set SAapp = New SA2001.Application, place your cursor over the SAapp object. Notice that a popup gives you a value of the object, SAapp = Nothing.
Using the immediate window
-
Click the Step Into button again, to highlight the line Set SAapp = Nothing.
-
Place your cursor into the Immediate window at the bottom of the IDE.
-
Type in the following:
? "Hello"
and hit Enter.
Notice that the word Hello is printed on the next line in the Immediate window. The question mark is a Visual Basic print command. You have just asked the Immediate window to print the word “Hello”, and it complied.
Getting the encyclopedia
We will use these VBA tools as we code. Let's now get information from the SAapp object concerning an encyclopedia's name. Take another look at System Architect’s object model, at the Encyclopedia object. Notice that it has an attribute called FullName. Let's get it.
First we'll use the Immediate window to test our code; this is a quick way to make sure our code is correct. If it furnishes an acceptable answer, we'll add it to our subroutine.
-
In the Immediate window, type the following:
? sAapp.
(make sure you type the 'period')
and from the ensuing drop-down list, select encyclopedia
-
Type in a period (.) after encyclopedia. VBA’s intellisense gives you a drop-down of all properties and methods of the encyclopedia object.
-
Select FullName. Your final code in the immediate window should look as follows:
? sAapp.Encyclopedia.FullName
Hit Enter. VBA provides you with the current encyclopedia path in the Immediate window.
Now that we know this code works, we can add it to our subroutine. We'll use the Debug command, which is an internal VBA object that has a print method. The Debug.Print object and method will replace the ? that we used in the Immediate window:
-
In the code, just below the line Set SAapp = New SA2001.Application, type/select the following:
Debug.Print SAapp.Encyclopedia.FullName
-
Step through the code, and place your cursor on the SAapp object. You will get the encyclopedia path printed to the Immediate window.
Finally, let’s add some text to the print statement, so that in the future we know that we are printing the encyclopedia’s path/name.
-
Change the above line to read:
Debug.Print "Encyclopedia = " & SAapp.Encyclopedia.FullName
Now when you step through the code, the text “Encyclopedia =” is added in front of the encyclopedia path and name.
Next tip
The encyclopedia object in itself does not give us too much interesting information. However, in the next tip, we will look at three objects it is related to – the Symbol, Definition, and Diagram objects – to get more information from the encyclopedia.
- Continue with the next tip, or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.