A developer can think of documents in two ways. First, there's the content—the text, tables, charts, shapes, pictures, and so on. Second, there's how the user interacts with the content using the mouse, the keyboard, and the tools on the menus and toolbars when a document is displayed in a window. When you start an Office application and open a document, you see the document in a window. You navigate to a specific part of the document, click where you want to add or remove content using the mouse, and then use the keyboard to type text or click buttons on a toolbar to format the content, for example.
When you interact with content through the mouse and keyboard, you trigger, or "fire" events in the background. Each Office application tracks the interaction, determining what document is displayed and what you've selected, clicked, right-clicked, or double-clicked. Once it determines the specific content you're interacting with, the application updates its menus and toolbars, displays a specific dialog box or shortcut menu, or updates a status bar.
As a developer, you can handle the same events the application does, and thus you can customize what happens when the user interacts with the content. This chapter describes the events that the Office application fires; common scenarios that you can address in your solution with them; and ways you can customize the user's experience with custom dialog boxes, shortcut menus, and menu and toolbar customizations.
NOTE
All the events described in this chapter are triggered whether the event occurs in the user interface or programmatically through the object model (using Microsoft Visual Basic for Applications).
The following steps show a simple tip for debugging and testing your solution and determining exactly when an event procedure is called. Always remember to create a new class module and declare an object of type Application before using the application's events.
Public WithEvents App As Application |
Once you press ENTER, the new App object, declared with events, appears in the Object drop-down list in the class module. When you select the new object in the Object drop-down list, the Procedure drop-down list shows the events for the Application object.
Static i As Integer i = i + 1 Debug.Print "EventProcedureName: " & i |
These three lines are great for debugging your solution and determining exactly when the event procedure is called. After everything is set up in step 5, the event should fire every time you manipulate the content. You use the static integer variable i so when the text from the code line Debug.Print is printed to the Immediate window in the Visual Basic Editor, you can see a change from line to line in the printed text.
You should change the text "EventProcedureName" to the procedure name of the event you selected from the Procedures drop-down list in step 2. In this example, the text would be replaced with "WindowSelectionChange."
Dim AppEvents As New Class1 Sub InitEvents() Set AppEvents.App = Application End Sub |
Before an event procedure will run, you must connect the declared object App in the class module to the Application object. Note that the class name "Class1" stated just after the New keyword in the declaration statement should be the name of the class module that you declared the App variable to be in step 2. By default, the class module inserted in step 2 is Class1, but you can change the name of the class module to anything you like.
In the Immediate window, you should see the text "WindowSelection-Change" followed by a number. Each time an action occurs such that the selection changes in the active document window in Word, the number following the text "WindowSelectionChange" is changed by one.
No matter what event you write code for, you should use this technique to help you debug and test your solutions. It also helps you determine exactly when an event is fired. Later in this chapter you'll learn more about each event that's fired as a result of a user's interaction with and manipulation of the active document's content. You can either use the lists from each of the descriptions for each event or use the technique described in this example to determine when an event fires.