This tip is the second 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. You will 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 introduced VBA and looked at the System Architect object model. In this tip we'll create a macro project, create a subroutine within it, and begin our coding by using the highest-level object of the object model - the Application object. This will set the wheels in motion for coding that we will do in ensuing tips.
Create a new macro project
- In System Architect, select Tools, Macros, Macro Projects.
- In the Macro Projects dialog, press the Add button.
- In the Open Macro Project dialog, make sure you are pointing at System Architect’s main directory. We will create a new macro project called Tutorial.mac.
- In the File Name: text box, type in Tutorial. The Files of type property should read System Architect Macro Project (.mac). Be sure to toggle off the Open as read-only choice at the bottom of the dialog, and press Open.
- In the Macro Projects dialog, with the Tutorial project selected, press the Apply button. Press Yes to any dialog that comes up.
- Press OK to close the Macro Projects dialog.
Add code to the macro
Now let’s open this Tutorial project and add code to it.
- Select Tools, Macros, VBA Editor. The VBA Interactive Development Environment will open.
- Right-hand mouse click on the Tutorial project itself - you can see that we can insert various types of objects:
- Forms
- Modules
& Class Module (can create classes you can access from other applications) 3. Choose to insert a Module.
- In the first row of the Alphabetic tab of the Properties window on the left-hand side, you can type in the name of this module, which by default is Module1. Overwrite Module1 with Example.
Using the general workspace
Notice that the top-right of the VBA IDE contains a General workspace, and declarations. This is where we will create and store dimension variables that are accessible by the module, and where we will type our code.
Create a subroutine
Let's first create a subroutine within this macro. Within the subroutine we'll place the code that will get information from the open System Architect encyclopedia.
-
In the General workspace, type in the following:
Sub Main()
and hit Enter
-
Note that the end of the subroutine is automatically added:
End Sub
Use the application object of the object model
In the previous tip, we looked at the object model. Let's take a closer look at it now - look at the Application class in the diagram below. Notice that an application can have zero or one encyclopedias.
Application is the highest level object in the object model. It is the starting point for everything. The same holds true for all applications for any object model – Word has an application object, Excel has an application object, etc.
Dimensioning an object
Let’s dimension an Object. Dimensioning an object makes space for it in memory.
-
Type in the following:
Dim SAapp As
and hit the spacebar.
As soon as you type the spacebar, VBA’s intellisense gives you a drop-down list of items. One of the things you see in the drop-down is a library called sa2001.
-
Select sa2001
-
Type in a period (.), now the drop-down intellisense window gives you all objects available in the SA2001 library. Choose the Application object. Your final line of code should read:
Dim SAapp As SA2001.Application
Dim means we will make some space in memory for the application object. It doesn’t mean it is active.
Creating an active instance of the application object
To make the Application object active we have to create an instance of this object:
-
Type in the following, selecting where appropriate in the Intellisense drop-down selections:
Set SAapp = New SA2001.Application
Ending the instance
It is good programming practice to kill all of your objects, or release them, at the end of the routine by setting them to Nothing. If you don’t, each object will remain active until you close Windows; the more objects you have active the more memory you take up, and at some point an overload will cause programs to freeze.
-
Before the End Sub line, type in
Set SAapp = Nothing
Your code should look as follows:
Next tip
That's the end of this tip. So far we've created a macro project, created a subroutine within it, dimensioned the all-important Application object, and created an active instance of the Application object. Our next step will be to get information about the active encyclopedia, which we'll do in the next tip.
- Continue with the next tip, or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.