Creating Events and Event Procedures
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.
As a Microsoft® Visual Basic® for Applications (VBA) programmer, you understand how to handle form and control events. If you have programmed in Microsoft® Word or Microsoft® Excel, you might also have taken advantage of events on the ThisDocument, ThisWorkbook, or SheetN objects. These event procedures are simple to create — you just open the class module and construct the event procedure from the Object and Procedure drop-down lists in the Code window.
There are two additional ways to handle events from VBA code in a Microsoft® Office application. You can create event procedures for certain objects that provide built-in events but that do not have associated class modules. In addition, you can create custom events for your own classes.
In This Section
- Creating Event Procedures for Built-in Events by Using the WithEvents Keyword
The objects for which you have commonly written event procedures — UserForms, Microsoft® Access forms and reports, and the ThisDocument, ThisWorkbook, and SheetN objects — all have an associated class module. - Adding Events to Custom Classes
You can include your own events in custom classes, and create event procedures that run when those events occur.
Related Sections
- Custom Classes and Objects
If you have never used class modules to build custom objects before, this section covers the concepts that you must understand to design, build, and use custom objects with their own methods and properties. - Why Build Your Own Objects?
Build entire custom object models that involve complex code behind the scenes, but that present a relatively simple and intuitive object syntax to the programmer. - Basic Class Concepts
Become familiar with using class modules to build custom objects, and learn the basics of adding a class to your project, creating an instance of a class in memory, and constructing properties and methods. - Creating Property Procedures
Public module-level variables in a class module function as properties of an object. - Extending Objects Through Interfaces
Suppose that in the process of designing your application, you decide that you want to create several objects that are closely related, and, in fact, require at least some of the same properties and methods. - Designing Object Models
When you design an object model, you are taking abstract processes and imposing concrete relationships upon them. - Creating Custom Objects for Web Pages
There are two ways to create objects for Web pages that are similar to custom objects created in Microsoft® Visual Basic® for Applications (VBA): by creating scriptlets, and by using Microsoft® Internet Explorer version 5 and later behaviors.