Programming Solutions with VBA

3 4

What is Visual Basic for Applications 6.3 (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-19 shows.

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

figure 21-19.  in the vba window, you can write a macro program to control an object in visio.

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

InsideOut


You can enable and disable VBA in Visio with the Options command, which also includes options for enabling COM (Component Object Model) 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 standalone 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 new 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. For example, Microsoft Office Developer Edition comes with a personal certification tool. The first time a user opens a diagram containing a signed project, the user is prompted to accept the certificate.

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:

  • In Visio, choose Help, Developer Reference to display the Visio Developer Reference. You'll find some conceptual information about using Automation with the Visio object model and a complete reference of the events, methods, objects, and properties that you can use.
  • In the VBA window, choose Help, Microsoft Visual Basic Help for help on using the editing tools in the VBA environment. This is generic Visual Basic help and doesn't tell you about any Visio-specific options.
  • On the Web, go to http://msdn.microsoft.com, the technical site for developers, and type "Visio" in the Search box. The MSDN site includes the complete text of Developing Microsoft Visio Solutions, the definitive guide to the Visio development platform, as well as a wealth of resources and technical articles about Automation, Visual Basic, and more.



Microsoft Visio Version 2002 Inside Out
Microsoft Visio Version 2002 Inside Out (Inside Out (Microsoft))
ISBN: 0735612854
EAN: 2147483647
Year: 2000
Pages: 211
Authors: Nanette Eaton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net