Volume 3: Programming with the Visio Object Model disclaimer

Download 0.78 Mb.
Size0.78 Mb.
1   2   3   4   5   6   7   8   9   ...   36

Exercise 1

This exercise will use VBA to declare some variables, assign numbers to them, and then perform some simple mathematical operations on them.


  1. Open the VBA window in Visio (Alt-F11).

  2. In the VBA window add a new module to the project using the menu Insert > Module. You can give the new module a name or leave it as the default name.

  3. In the Project window, double click the module name. This will display the code window for the new module.

  4. Click in the code window and choose the menu Insert > Procedure. Give the new procedure a meaningful name such as “Excercise1”. Set Type to Sub and Scope to Public. Choose OK to close the dialog.

  5. Add the following code to the new aubroutine.

Public Sub Exercise1()

'Dimension the Variables

Dim intNumber1 As Integer

Dim intNumber2 As Integer

Dim intNumber3 As Integer

'Assign them values

intNumber1 = 10

intNumber2 = 20

intNumber3 = intNumber1 + intNumber2

'Display the operations in the Immediate widow

Debug.Print "intNumber1 = " & intNumber1

Debug.Print "intNumber2 = " & intNumber2

Debug.Print "intNumber3 = " & intNumber3

End Sub

Other Operations

Logical Operators

The three most common logical operators are:

  • AND

  • NOT

  • OR

These are mostly used in Conditional statements (IF THEN ELSE), which will be discussed shortly.
Concatenating Variables

Often times you will want to combine strings together, a process called concatenation. You concatenate strings together in VB with the ampersand character (&). The following code combines two strings together, with a space added in between:

Dim strPeter as String

Dim strPan as String

Dim FullName as String

strPeter = “Peter”

strPan = “Pan”

strFullName = strPeter & “ “ & strPan

Exercise 2

This exercise will use VBA to declare some variables as strings, assign values to them, and then concatenate them.


  1. Add another subroutine to your coding module. Call it “Exercise2”.

  2. Add the following code to the new subroutine:

Public Sub Exercise2()

'Dimension the Variables

Dim strNameFirst As String

Dim strNameMiddle As String

Dim strNameLast As String

Dim strNameFull As String

'Assign them values

strNameFirst = "Thomas"

strNameMiddle = "Alva"

strNameLast = "Edison"

strNameFull = strNameFirst & " " & strNameMiddle & _

" " & strNameLast

'Display the results in the Immediate window

Debug.Print "The full name is:" & strNameFull

End Sub


The underscore character at the end of the statement assigning a value to strNameFull is a line continuation charater in VBA. It signifies that the code for this statement wraps to the next line.

Data type conversions and variable scope


Often times you will have to convert values in order to work with them better. This might mean converting a decimal number into a whole number, or a numerical sequence into a string. You can do this through several conversion functions that are built into VBA. There are many conversion functions, and we will not cover all of them in this supplement. We will only look at two functions, CInt and CStr.

The CInt function will convert a number variant into an integer and rounds it to the nearest even number. The CStr function will convert an integer into a string subtype.

Variable Scope

Variables can be either local or global in scope. The variables we have seen so far have been global in nature, that is, they are valid for the entire block of code that we have written. Sometimes you may wish to break your code into functions or procedures, which are blocks of code that you can reuse throughout your application. In this case, if you declare a variable in a procedure or a function, the variable is only valid for the time that the procedure or function is executed.


Global variables must be defined at the top of the module before any subroutines or functions are defined.


This exercise will demonstrate local variables that are used in sub-procedures.


  1. Define the global variable. This must be done outside of any subroutine or function and must be defined at the top of the module before any subroutines or functions are defined

Dim strFirst as String

  1. Add the following code to the module.

Sub Procedure_1()

strFirst = "I'm a string in procedure 1"

Debug.Print "Calling Procedure 1:" & strFirst

End Sub

Sub Procedure_2()

strFirst = "I'm a string in procedure 2"

Debug.Print "Calling Procedure 2:" & strFirst

End Sub

Public Sub Exercise3()

'Display the results in the Immediate window



End Sub

  1. To run the code, click within subroutine Exercise3 and choose hit F5. The results are displayed in the Immediate window.

Download 0.78 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   ...   36

The database is protected by copyright ©ininet.org 2022
send message

    Main page