Creating a VBA Project
A Microsoft® Visual Basic® for Applications (VBA) project consists of modules, class modules, and user forms.
- A module is a set of declarations followed by proceduresa list of instructions that a program performs.
- A class module defines an object, its properties, and its methods. A class module acts as a template from which an instance of an object is created at run time. Every Visio-based VBA project contains a class module called ThisDocument; this class module represents the properties, methods, and events of the Microsoft® Visio® document that contains the VBA project.
- A user form contains user interface controls, such as command buttons and text boxes.
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:
- Validates the user's input
- Translates the code into data that could be used to generate a drawing
- Uses Automation to create the drawing in the Visio drawing window
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 http://msdn.microsoft.com/en-us/library/office/aa201799(v=office.10).aspx Chapter 24, Using ActiveX Controls in a Visio Solution.
In this section
Inserting Modules and Class Modules into Your Project
Many VBA programs contain one or more modulesa 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.
To insert a module or class module
- In the Visual Basic Editor, on the Insert menu, click Module or Class Module.
- window will display an empty module where you will insert procedures that create templates into which you enter VBA code.
To add procedures to modules and class modules
- In the Visual Basic Editor, on the Insert menu, click Procedure to open the Add Procedure dialog box.
- In the Name box, name the procedure.
- The procedure's name is displayed on its module's submenu on the Visio
- submenu on the
- menu. A procedure name cannot include spaces or reserved words (such as
- , or
- ) that VBA uses as part of its programming language.
- Under Type, choose the type of procedure: Sub, Function, or Property.
- To write a procedure that takes no arguments, insert a Sub procedure.
- To write a function that takes arguments and returns a value, insert a Function procedure.
- To add properties to a class module, insert a Property procedure.
- Under Scope, choose Public or Private.
- A procedure with a private scope can be accessed by only the module that contains it; only a procedure within the same module can call a private procedure, and a private procedure does not appear on any menus or in any dialog boxes.
- A procedure with a public scope can be accessed by other programs and modules. Visio displays public procedures of modules and the ThisDocument class module that take no arguments on the Macros submenu.
- To declare all local variables as static, select the All Local variables as Statics check box.
- Static variables exist for the lifetime of your entire program.
- Local variables exist only while the procedure that they are declared in is running. The next time the procedure is executed, all local variables are reinitialized. However, you can preserve for the lifetime of your program the value of all local variables in a procedure by making them static, which fixes their value.
- Click OK.
- VBA inserts a
- procedure template
- into the item's
- 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.
Inserting User Forms into Your Project
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.
To add a user form to your project
- On the Insert menu, click UserForm.
- From the Toolbox, drag the controls that you want onto the user form.
- Double-click a user form or control to display its Code window.
- Click the event that you want to respond to in the drop-down list of events and procedures in the Code window; you can then start typing your code. Or, insert a procedure and start typing your code in the procedure template.
Tip This guide uses the VBA default user form and control names for clarity, but it is considered good programming technique to change the default names to something more descriptive. Many programmers use the following naming conventions:
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.
Importing Files into and Exporting Files from Your Project
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 You cannot drag the ThisDocument class module between Visio files, but you can drag (or copy and paste) code from ThisDocument into other project items.