Chapter 21, Handling Visio Events

Chapter 21, Handling Visio Events

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

An event is an action or occurrence, often generated by a user, that your program might respond to. In Microsoft® Visio®, events can result from user actions such as opening or closing documents, dropping or deleting shapes on the drawing page, editing the text of shapes, and altering shape formulas. Knowing that such events have occurred can be extremely useful because it allows you to program your solution to respond to user actions that can otherwise be difficult to predict.

You can handle events either programmatically or by using Visio formulas. As with any Visio solution, you should start by putting as much of the functionality as possible in formulas. For details about handling events using formulas, see Chapter 7, Enhancing Shape Behavior.

Visio provides two separate ways to handle events from your program:

  • In Microsoft® Visual Basic® for Applications (VBA) or Microsoft® Visual Basic® programs, the simplest approach is to use the WithEvents keyword to declare object variables that can receive events. This is called writing code behind events.
  • You can create Visio Event objects that can run an add-on or advise your program that an event has occurred. This technique can be used from any development environment. (In versions of VBA or Visual Basic earlier than 5.0, this was the only means of handling Visio events through Automation.) You can also set event filters to tailor the events that your solution listens to, and prevent many of the events that you want to ignore from entering the event queue.

For details about handling events in a C++ program, see Chapter 28, Programming Visio with C++.

In this chapter…

An Event Overview

Writing Code Behind Events

Visio Event Objects