3 4
A Microsoft Visual Basic for Applications (VBA) project consists of modules, class modules, and user forms.
A simple project might require a single user form or module, although more complex projects might contain multiple modules, class modules, and user forms. For example, a solution that generates a drawing could include a user form to gather information from the user. It could also include code, organized in one or more modules and/or class modules, that does the following:
If the solution should run in response to an event, such as opening a document, the solution could include code that would run when that event occurred.
You can add existing modules, class modules, or user forms to your project by importing files; or, you can export project items for use in other projects. You can also insert an ActiveX control into your Visio document. For details about ActiveX controls, see Chapter 24, Using ActiveX Controls in a Visio Solution.
Many VBA programs contain one or more modules—a set of declarations followed by procedures. Every Visio VBA project contains the class module ThisDocument, which is an object that represents the project's document. You can create additional class modules to define custom VBA objects in your project.
The Code window will display an empty module where you will insert procedures that create templates into which you enter VBA code.
Figure 15-4 The Add Procedure dialog box .
The procedure's name is displayed on its module's submenu on the Visio Macros submenu on the Tools menu. A procedure name cannot include spaces or reserved words (such as MsgBox, If, or Loop) that VBA uses as part of its programming language.
Modules and class modules can contain more than one type of procedure.
Scope is the extent to which a procedure can be accessed by other modules and programs.
You can declare the variables in your procedure as local or static (global).
VBA inserts a procedure template into the item's Code window into which you can enter code. The template contains the first and last lines of code for the type of procedure you insert.
For details about procedures, see the Microsoft Visual Basic Help.
If you want your program to prompt the user for information, you can build a user interface by inserting user forms. A user form contains user interface controls, such as command buttons and text boxes. When you add a user form to your project, VBA automatically opens the Toolbox of controls. A control is an object you place on a user form that has its own properties and methods; a control also fires events that you can respond to. You use controls to receive user input, display output, and trigger event procedures.
Figure 15-5 Toolbox and user form containing controls.(A) Controls tab in the Toolbox. (B) User form. (C) Text box control. (D) Command button control.
For details about adding controls, such as command buttons and text boxes, see the Microsoft Visual Basic Help.
Tip
User form default name = UserForm1 Revised name = frmGetDocName
Notice the use of frm in the revised name of the user form. In the control name, many programmers use frm (form), txt (textbox), lbl (label), cmd (command button), and so on, so you can quickly recognize the type of object.
To import an item into your project, on the File menu, click Import File. You can choose any VBA module (files with a .bas extension), user form (files with an .frm extension), or class module (files with a .cls extension) to add a copy of the file to your project. To export an item from your project so that it will be available for importing into other projects, select the item you want to export in the Project Explorer, then on the File menu, click Export File and enter the location in which you want to save the file. Exporting an item does not remove it from your project.
You can also drag projects or project items between Visio files by selecting the project or project item you want to move in the Project Explorer and then dragging its icon onto a Visio project icon. A project item is automatically stored in the correct project folder. A project is referenced in the References folder, because a Visio file can contain only one project, but that project can reference other projects.
Note