Programming Solutions with VBA

What is Visual Basic for Applications 6.5 (VBA) doing in Visio 2007? 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 2007. You can actually automate Visio 2007 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 2007, choose Tools, Macro, 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 2007.


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 2007 has an open architecture, which means that Visio 2007 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 2007 can do, you can do programmatically through the object model. In fact, the Visio 2007 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 2007 customization is creating automated sales tools that can generate a purchase order or bill of materials from a Visio 2007 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 2007. VBA macros can scan the equipment in the drawing to create a bill of materials or a purchase order on the spot.

Inside Out: Enabling and disabling VBA

You can enable and disable VBA in Visio 2007 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 2007. However, you can also create an add-in as an executable (.exe) file that can be launched from Visio 2007 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++ or C# programmer, you can also create an add-in as a Visio 2007 library (.vsl), a special type of dynamic-link library (DLL) that is loaded by Visio 2007 at run time. This is how most wizards in Visio 2007 are created—if you look in the Solutions folders, you’ll see many files with the .vsl extension.

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

Note

If you want to add a macro or add-in to a Visio 2007 document, you might want to investigate the digital signature support in Visio 2007. 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 2007, you can take advantage of the many Web-based resources that Microsoft makes available. The Microsoft Visio 2007 team also produces very thorough documentation for developers, some of which is delivered in the Developer Reference in the Help system that comes with the product, and the rest of which is available on the Web.

Here are some places to look for more information:

  • In Visio 2007, choose Help, Developer Reference to display the Visio Developer Reference. You’ll find some conceptual information about using Automation with the Visio 2007 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 (although it has not yet been updated for Visio 2007), as well as a wealth of resources and technical articles about Automation, Visual Basic, and more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset