Microsoft Office Visio 2003 Inside Out (Inside Out (Microsoft))

What is Visual Basic for Applications 6.4 (VBA) doing in Visio? Think like a programmer for a minute. What if every action that you can do to shapes could be done automatically? What if there was a way to collect information from a drawing, such as through a form or an interactive control? What if the information in a drawing could be sent to another program for analysis automatically? With VBA, you can write a separate program called a macro that controls actions in Visio. You can actually automate Visio using any development tool that’s capable of creating Microsoft Windows applications, but VBA is the tool that’s built in. To display the VBA editor in Visio, choose Tools, Macros, Visual Basic Editor. The VBA editor appears, as Figure 21-18 shows.

Figure 21-18: In the VBA window, you can write a macro program to control an object in Visio.

If you’re not a programmer, it might be a little hard to imagine what you can do in the VBA window. If you are a programmer, you might become frustrated by the lack of detail in this book about what you can do. The thing to know is that Visio has an open architecture, which means that Visio exposes its objects, such as windows, drawing pages, shapes, layers, menus, and toolbars, through a structured interface called the object model. Just about anything that Visio can do, you can do programmatically through the object model. In fact, the Visio programmers at Microsoft use the same tools that are available to you to create much of the automated behavior in many templates.

For example, one popular area of Visio customization is creating automated sales tools that can generate a purchase order or bill of materials from a Visio drawing. Custom shapes are linked to database information about the properties of the company’s equipment. Sales personnel can then use the shapes to prototype configurations for prospective clients in Visio. VBA macros can scan the equipment in the drawing to create a bill of materials or purchase order on the spot.

Inside OutEnabling and disabling VBA

You can enable and disable VBA in Visio with the Options command, which also includes options for enabling Component Object Model (COM) add-ins and Automation events. Choose Tools, Options, and then click the Advanced tab to see the developer settings.

About Writing Add-Ins

The easiest way to create an add-in is to create a public procedure in a VBA module. VBA code is stored as part of the drawing file and runs in the same memory space as Visio. However, you can also create an add-in as an executable (.exe) file that can be launched from Visio or executed as a stand-alone program, and you can write the add-in using any programming language that supports COM. Executable files run in their own memory space. If you’re a C++ programmer, you can also create an add-in as a Visio library (.vsl), a special type of dynamic-link library (DLL) that is loaded by Visio at run time. This is how most wizards in Visio are created—if you look in the Solutions folders, you’ll see many files with the .vsl extension.

Visio includes a type library, a file that contains Automation descriptions of the objects, properties, methods, events, and constants that Visio exposes to Automation controllers. The Object Browser in the VBA window (View, Object Browser) displays the Visio Automation descriptions and includes code templates that you can copy.

Tip

Use digital signatures with Visio If you want to add a macro or add-in to a Visio document, you might want to investigate the digital signature support in Visio. You can digitally sign your VBA projects using a signature that can be obtained from any number of certification companies.

Getting More Information

There are so many guides for programming with VBA that this book doesn’t try to tell you how to write code. However, if you want to find out more about what you can do with Visio, you can take advantage of the many Web-based resources that Microsoft makes available. The Microsoft Visio team also produces very thorough documentation for developers, some of which is delivered in reference form with the product, and the rest of which is available on the Web.

Here are some places to look for more information:

Категории