Walkthrough: Hooking Events in Word

 

Ken Getz
MCW Technologies, LLC

September 2003

Applies to:
    Microsoft® Visual Studio® Tools for the Microsoft Office System
    Microsoft Office Word 2003
    Microsoft Visual Studio .NET 2003

Summary: Microsoft Visual Studio Tools for the Microsoft Office System makes it easy to integrate managed code with events raised by Word 2003 objects. This walkthrough demonstrates reacting to the WindowDeactivate event raised by the Word Application object. (5 printed pages)

Contents

Introduction
Prerequisites
Getting Started
Setting Up the Event Handler
Adding the Event Code and Testing
Conclusion

Introduction

In this walkthrough, you'll use the tools provided by Microsoft® Visual Studio® .NET and Microsoft Visual Studio Tools for the Microsoft Office System to create a new Microsoft Office Word 2003 document. You'll add code to react to the WindowDeactivate event of the Word Application object, causing the deactivated window to minimize.

Prerequisites

To complete this walkthrough, the following software and components must be installed on the development computer:

  • Microsoft Visual Studio .NET 2003 or Microsoft Visual Basic® .NET Standard 2003
  • Microsoft Visual Studio Tools for the Microsoft Office System
  • Microsoft Office Professional Edition 2003

**Tip   **This demonstration assumes that if you're a Visual Basic .NET programmer, you've set the Option Strict setting in your project to On (or have added the Option Strict statement to each module in your project), although it is not required. Setting the Option Strict setting to On requires a bit more code, as you'll see, but it also ensures that you don't perform any unsafe type conversions. You can get by without it, but in the long run, the discipline required by taking advantage of this option far outweighs the difficulties it adds as you write code.

Getting Started

To get started, you need to create a Visual Studio .NET project that works with Microsoft Office Word 2003.

To create a Word Document project

  1. Start Visual Studio .NET, and on the File menu, point to New, and click Project.

  2. In the Project Types pane, expand Microsoft Office System Projects, and then select Visual Basic Projects or Visual C# Projects.

  3. In the Templates pane, select Word Document.

  4. Name the project WordEvent, and store it in a convenient local path.

  5. Accept the defaults in the Microsoft Office Project Wizard, and click Finish to create the project and the new Word document.

    Visual Studio .NET opens the ThisDocument.vb or ThisDocument.cs file in the Code Editor for you.

The wizard populates the solution with two files:

  • AssemblyInfo.vb or AssemblyInfo.cs, which stores assembly-level metadata.
  • ThisDocument.vb or ThisDocument.cs, which contains your code that responds to Word events.

Setting Up the Event Handler

The OfficeCodeBehind class created by the Visual Studio template includes event procedures that allow you to handle the Word document's Open and Close events—simply add code to the existing procedures. In this exercise, you'll write code that reacts to an event that isn't already handled by the template code, the WindowDeactivate event. You'll add the event procedure, hook up the event handling (if you're coding in Microsoft Visual C#®), and react to the event by minimizing the deactivated document. Select the appropriate section below, depending on your choice of language.

(Visual Basic Only) Set Up the Event Procedure

In order to start your code running, you'll need to react to the Application.WindowDeactivate event. In this section, you'll add support for reacting to this event in Visual Basic .NET.

To create the event handler (Visual Basic)

  1. From the Class Name drop-down list in the upper-left corner of the Code Editor, select ThisApplication.

  2. From the Method Name drop-down list in the upper-right corner of the Code Editor, select WindowDeactivate.

    Visual Studio .NET creates the event handler stub for you (wrapped to fit this space):

    ' Visual Basic
    Private Sub ThisApplication_WindowDeactivate( _
        ByVal Doc As Microsoft.Office.Interop.Word.Document, _
        ByVal Wn As Microsoft.Office.Interop.Word.Window) _
        Handles ThisApplication.WindowDeactivate
    
    End Sub
    

(C# Only) Set up the Event Procedure

In order to start your code running, you'll need to react to the Application.WindowDeactivate event. In this section, you'll add support for reacting to this event in C#. Although the code created by the Visual Studio .NET template uses a slightly more complex method to hook up the event, the steps in this section add event handlers that use a simpler technique.

To create the event handler (C#)

  1. Add the following procedure stub to the class created for you by Visual Studio .NET, OfficeCodeBehind:

    // C#
    protected void thisApplication_WindowDeactivate(
        Word.Document doc, Word.Window Wn)
    {
    }
    
  2. In the ThisDocument_Open procedure, type the following code. Once you've entered the code, you'll see a ToolTip. Press the TAB key, as instructed by the ToolTip, to complete the line of code:

    // C#
    thisApplication.WindowDeactivate +=
    // Once you've finished, the code will look like this, wrapped
    // to fit this space:
    thisApplication.WindowDeactivate +=
        new Microsoft.Office.Interop.Word.
        ApplicationEvents4_WindowDeactivateEventHandler(
        thisApplication_WindowDeactivate);
    

Adding the Event Code and Testing

Once you've set up the event handler, you can add the code that will react to the event. Follow these steps to add code that will minimize the deactivated window:

To minimize the deactivated window

  1. Modify the ThisApplication_WindowDeactivate procedure so the code looks like this:

    ' Visual Basic
    Private Sub ThisApplication_WindowDeactivate( _
        ByVal Doc As Microsoft.Office.Interop.Word.Document, _
        ByVal Wn As Microsoft.Office.Interop.Word.Window) _
        Handles ThisApplication.WindowDeactivate
    
        Wn.WindowState = Word.WdWindowState.wdWindowStateMinimize
    End Sub
    
    // C#
    protected void thisApplication_WindowDeactivate(
        Word.Document doc, Word.Window Wn)
    {
        Wn.WindowState = Word.WdWindowState.wdWindowStateMinimize;
    }
    
  2. Press F5 to run the project. This loads Microsoft Office Word with the document you've created.

  3. Select New Window on the Window menu to create a new window, and note that when you select the menu item, the current window minimizes. (This behavior isn't something you would want in an application, but it does demonstrate the event handling.)

  4. Try creating a few more windows to verify that the event-handling code is working properly.

  5. Close all windows, and return to Visual Studio .NET.

  6. Select Save All on the File menu to save your solution.

Conclusion

In this walkthrough, you reacted to the WindowDeactivate event raised by the Word Application object to cause the current window to be minimized. Although it might not be a behavior you would want in an application, it provides an example of how Visual Studio Tools for the Microsoft Office System makes it easy to integrate managed code with events raised by Word 2003 objects.