The Microsoft Knowledge Base contains many articles that contain
sample code for Visual Basic for Applications macros. Many of these macros
illustrate basic concepts of programming in Visual Basic for Microsoft
Visio. This article contains instructions on how to use the sample code to
create a new macro and how to run that macro.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Microsoft Visio stores macros in a module within a project that is stored in a template (.vst), a stencil (.vss), or a drawing (.vsd). To use the sample code in a Knowledge Base article, you must first add a new Visual Basic macro (or select an existing macro) in a Visio template, stencil, or drawing. Then you can paste or type the sample code into this macro.
Open or create a Visio template, stencil, or drawing as appropriate.
On the Tools menu, point to Macro, and then click Macros.
In the Macros In list, click the location or module where you want to save the macro.
In the Macro Name box, type a name for the macro.
Click Create to open the Visual Basic Editor.
Do one of the following:
Type the macro code between the Sub Macroname() line and the End Sub line.
Sub macroname()
Type your macro code here
End Sub
Copy and paste the sample code to your macro by following these steps:
Sub macroname()
End Sub
Switch to the program that you are using to view the Microsoft Knowledge Base article, such as Microsoft Internet Explorer.
To select the sample code, hold down the CTRL key and then single-click the code.
On the Edit menu, click Copy.
Switch to the Visual Basic Editor.
In the Visual Basic Editor, select the existing code text of Sub Macroname() to the End Sub of the macro.
On the Edit menu, click Paste.
NOTE: When you paste, the existing code text will be replaced with the code that you copied from the Knowledge Base article.
After typing or pasting the code for your macro, click Close and Return to Visio on the File menu.
NOTE: Use the TAB key to indent text. Lines that begin with an apostrophe (') are comments that are not required to run the macro. However, you may want to type comments to make it easier to understand what the macro is doing.
To add code to a module, you must either have an existing module or create
a new module. To do this, follow these steps:
Open or create a Visio template, stencil, or drawing as appropriate.
On the Tools menu, point to Macro and then click Visual Basic Editor.
In the Project window of the Visual Basic Editor (by default this is in the upper-left corner), click the project icon with the same name as your template, stencil, or drawing. If you have not yet saved your template or drawing, the project name will be similar to Project(Drawing1).
If the project that you clicked does not contain a folder called Modules underneath it, click Module on the Insert menu.
Click anywhere in the code window of the module that you want to create. The code window is usually in the upper-right corner of the Visual Basic Editor.
Type or paste the sample code exactly as it appears in the Knowledge Base article, including the Sub Macroname() and End Sub lines.
NOTE: For more information about typing or pasting the macro code, please see the "To Create a New Macro" section in this article.
After typing or pasting the code for your macro, click Close and Return to Visio on the File menu.
Microsoft Visio Developer Help contains numerous topics about working with macros. Visual Basic Help contains extensive information about writing your own code.
For additional information about getting help with Visual Basic for Applications, click the article number below
to view the article in the Microsoft Knowledge Base:
274481
(http://support.microsoft.com/kb/274481/EN-US/
)
Visio2000: Programming Resources for Visual Basic for Applications
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.