Using the Visual Basic Editor
To build your Microsoft® Visual Basic® for Applications (VBA) program, you create a VBA project using the Visual Basic Editor. Every Microsoft® Visio® document, or file, can contain a project that you can add modules and forms to, depending on what your solution requires. At a minimum, every project contains a ThisDocument class module. This class module represents the properties, methods, and events of a specific document associated with your Visio VBA project.
In the Code window, you can write, display, and edit code in new procedures or in existing event procedures. You can open any number of code windows on different modules, class modules, or user forms, so you can easily view the code and copy and paste between windows.
In this section
Starting the Visual Basic Editor
You can start the Visual Basic Editor without opening a Visio document, but you must first open a document to see the VBA project for that document. A Visio instance adds projects only to documents that are opened as Original or Copy; however, if the document already has a project, you can view the project for a document that is opened as Read only. A project is added to a document only if VBA project creation has been enabled in Visio. For details, see Enabling or Disabling VBA Project Creation later in this section.
To start the Visual Basic Editor
- Start Visio and open a template (.vst or .vtx), stencil (.vss or .vsx), or drawing (.vsd or .vdx) as Original or Copy, not Read only.
- On the Tools menu, point to Macros, and then click Visual Basic Editor. Or, click the Visual Basic Editor button on the Developer toolbar.
You can customize your working environment in VBA by setting options such as font size, code color, syntax error options, and variable declaration requirements.
To set environment options in the Visual Basic Editor
- On the Tools menu, click Options, click the Editor or Editor Format tab, and then set the options you want.
By default, projects will have the same name as the document with which they are associated. However, each project has a Project Properties dialog box where you can rename your project and provide additional information such as a project description. You can also lock your project.
To open the Project Properties dialog box
- On the Tools menu, click Project Name Properties, and then set project properties. Or, right-click the project name in the Project Explorer.
Navigating among Projects
To navigate among projects in the Visual Basic Editor, use the Project Explorer. It lists modules, class modules, and user forms for the projects in all open Visio files. You can double-click on any module, class module, or user form in the Project Explorer to open its Code window.
Note If you do not see the Project Explorer when you open the Visual Basic Editor, click Project Explorer on the View menu.
Saving a Project
A VBA project is stored in the Visio document that contains it. A Visio document can be saved as one of the following file types:
- Template (.vst or .vtx)
- Stencil (.vss or .vsx)
- Drawing (.vsd or .vdx)
When a user creates a new Visio document from a template, the Visio instance copies the VBA project and its items to the new document.
To save your Visio document and its VBA project
- In the Visio user interface, click Save on the File menu. Or, in the Visual Basic Editor, click Save File Name on the File menu.
- The document's file name and location are displayed in parentheses after the project name in the
- Project Explorer
- in the Visual Basic Editor.
Enabling or Disabling VBA Project Creation
In Visio, a VBA project gets created in your document whenever you open a new document (or a document that doesn't already have a project) while the Visual Basic Editor is open in Visio. Beginning with Microsoft Visio 2002, you have the option to suppress the creation of VBA projects while the Visual Basic Editor is open in Visio.
You enable or disable VBA project creation using the Enable Visual Basic for Applications project creation option on the Advanced tab in the Options dialog box. When this check box is selected, VBA projects are not automatically created for documents that don't already have VBA projects; however, VBA projects are still accessible in documents with VBA projects.
To disable VBA project creation
- On the Tools menu, click Options.
- On the Advanced tab, clear the Enable Visual Basic for Applications project creation check box.
- Click OK.