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.

Visual Basic Editor: the VBA development environment

Visual Basic Editor: the VBA development environment

  1. The Project Explorer displays a list of projects and project items in Visio documents.
  1. The Properties window displays a list of the properties for the selected item.
  1. The Code window
  1. The programming workspace displays all open modules, class modules, and user forms during design time. You build your program in this area.
  1. The menu bar displays the commands you use to build, run, and debug your program.
  1. The toolbar provides quick access to commonly used commands in the development environment.

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

Navigating among Projects

Saving a Project

Enabling or Disabling VBA Project Creation

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

  1. 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.
  1. On the Tools menu, point to Macros, and then click Visual Basic Editor. Or, click the Visual Basic Editor button Aa201750.bu_VBEditor(en-us,office.10).gif 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.

TOP

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.

The Project Explorer displaying three Visio files that are open in a Visio instance

The Project Explorer displaying three Visio files that are open in a Visio instance

  1. An example of a template and the items in its project
  1. An open Visio drawing and the items in its project; this drawing hasn't been saved
  1. An open Visio drawing, saved as Office Plan, and the items in its project

Note If you do not see the Project Explorer when you open the Visual Basic Editor, click Project Explorer on the View menu.

TOP

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.

An example of how a Visio drawing might look after adding items to the default project and saving the drawing

An example of how a Visio drawing might look after adding items to the default project and saving the drawing

  1. First Sub procedure in the module (macro)
  1. First Function procedure in the module (user-defined function)

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 Nameon 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.

TOP

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

  1. On the Tools menu, click Options.
  1. On the Advanced tab, clear the Enable Visual Basic for Applications project creation check box.
  1. Click OK.