About macros

If you want to become more familiar with Microsoft Visual Basic for Applications (VBA) code associated with the Microsoft Office Visio object model, or need to perform a repetitive development task in Microsoft Office Visio, you can record a macro to quickly create code that you can then view or modify. A macro consists of VBA code that is stored in a Visual Basic module.

For example, to learn the Visio object model, you might record various actions in Visio such as selecting a shape and setting its size and position on the page to get a sense of what code is involved. Or, you might want to set the page size and background for a document with many pages. Rather than going through the same, multiple steps for each page, you could record a macro for the modifications you make to one page, and then modify that macro to run and perform the same set of steps on each page in the document.

ShowRecording macros

When you record a macro, Visio stores information about each step you take as you perform a series of commands. You then run the macro to repeat, or "play back," the commands. VBA stores each macro in the NewMacros module in the VBA project of the Visio document (drawing, stencil, or template) you select.

ShowMaking macros easy to run

You can run a macro by choosing it from a list in the Macros dialog box. To make a macro run whenever you press a particular key combination, you can assign a keyboard shortcut to the macro.

ShowManaging macros

After you record a macro, you can view the macro code with the Visual Basic Editor to correct errors or change what the macro does.

The Visual Basic Editor makes writing and editing macro code easy, and provides plenty of online Help. With the Visual Basic Editor, you can edit macros, rename the modules that store the macros, or rename the macros.

 Note   To play a recorded macro, Visio must be in the same state (such as on the same page with the same shapes on the page) as when the macro was recorded. For example, you might record a macro on a new blank drawing page with one rectangle present on the page. If you play the recorded macro on the same page with two rectangles present instead of one, or if you play the macro on a different page, the macro might not run as you expect.

ShowMacro security

Visio provides safeguards that help protect against viruses that can be transmitted by macros. If you share macros with others, you can certify them with a digital signature so that other users can verify that they are from a trustworthy source. Whenever you open a drawing that contains macros, you can verify their source before you enable them.