Working with 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.

Events allow you to capture information when users perform certain tasks in Microsoft FrontPage. For example, when a user closes a Web site, you may want to publish the site. Responding to these events allows you to create custom applications that are intuitive. Using the Microsoft FrontPage Object Models, you can respond to events that are created by the FrontPage application (using the Application object model), a Web site (using the Web object model), or a page (using the Page object model). (For more information on the FrontPage Object Models, see About the FrontPage 2002 Object Models in this Software Development Kit.)

Declaring and Initializing Event Handler Variables

When you work with events, you must declare then with the WithEvents keyword inside a Class module. To do this:

  1. Start FrontPage.
  2. Press Alt+F11 to start the Microsoft Visual Basic Editor.
  3. On the Insert menu, click Class Module to create a new class module.
  4. Declare your event variables. These are global variables that you place at the top of your class module.

Private WithEvents objAppEvents as FrontPage.Appliction Private WithEvents objWebEvents As FrontPage.WebEx Private WithEvents objWebWindowEvents As FrontPage.WebWindowEx Private WithEvents objPageWindowEvents As FrontPage.PageWindowEx Private WithEvents objAnchorEvent As FPHTMLAnchorElement

  1. Once you declare the variables, you will be able to access the list of events in the Procedure list. To do so, select the object that you want to work with in the Object list.
  2. Lastly, you need to add a class initialize event routine. Do do this, click class in the Object list and click Initialize in the Procedure list.  Then enter the code to set the event handler variable equal to the object, as shown below.

Private Sub Class_Initialize() Set objAppEvents = FrontPage.Application End Sub

You also need to initialize your class module from a general code module. If in the above procedure, you created a new class module and accepted all the defaults, you would have a class module called Class1. To initialize this class module, do the following:

  1. Create a general code by clicking Module on the Insert menu.

  2. Create a global variable to handle the new class.

    Dim cls As Class1
    

  3. Write an initialization subroutine.

Sub InitializeClass1() Set cls = New Class1 End Sub

  1. Run the initialization subroutine by positioning the cursor inside it and pressing F5. If you step through it by pressing F8, you will notice that when the cls class variable initializes, it runs the Initialize event routine in the Class1 code module.

Working with the Application Object Model Events

There are a variety of application events for which you can create code to handle. First, you must declare and initialize the event handler, as shown above.  Next, you need to access the event for which you want to write code. To do this,

  1. Select the event handler variable in the Objects list.
  2. Click on the event for which to create code in the Procedures list.

For example, if you wanted to run code every time the active document closed, you would select the OnPageClose event. The following sample uses the OnPageClose event to display a message to determine if the user really wanted to close the active page window. If the user clicks No, the page close event is canceled.

  Private Sub objAppEvents_OnPageClose(ByVal pPage As PageWindow, _
        Cancel As Boolean)

    If (MsgBox("You are closing the active page. " & _
            "Are you sure you want to do that?", _
            vbYesNo, "Close page?") = vbNo) Then
        Cancel = True
    End If

End Sub

Working with the Web Object Model Events

You can create two types of events handlers for Web sites. One deals with general Web events and the other deals with Web window events.

Web Events

Some of the events included in Web events are OnBeforePublish, OnAfterPublish, and OnClose. These events deal with the entire Web and not just the items in the Web window, which the Web window events handle.

To illustrate, let's say you want to write special code to handle when a user closes a Web site. You would use the OnClose event. The following example displays a message to determine if the user really wants to close the current Web site. If the user clicks No, the event is canceled.

  Private Sub objWebEvents_OnClose(pCancel As Boolean)

    If (MsgBox("You have chosen to close the current " & _
            "Web. Are you sure you want to do that?", _
            vbYesNo, "Close page?") = vbNo) Then
        pCancel = True
    End If

End Sub

Web Window Events

Web window events handle events related to the FrontPage Web window, such as the current view. The OnBeforeViewChange and OnAfterViewChange events handle processes for when a user switches from one view to another. The following example displays a message when a user switches view to determine if a user really wants to switch to the new view. If the user clicks No, the event is cancelled.

  Private Sub objWebWindowEvents_OnBeforeViewChange(ByVal _
        TargetView As FpWebViewModeEx, Cancel As Boolean)

    If (MsgBox("You have chosen to close switch the current " & _
            "view. Are you sure you want to do that?", _
            vbYesNo, "Close page?") = vbNo) Then
        Cancel = True
    End If

End Sub

Note   This is Web views not page views, which are handled by page window events. Instead the OnBeforeViewChange Web event fires when you switch from Page view to Reports view.

Page Window Events

Page window events handle events that relate to the page view; for example, when a page window is closed, saved, or the page view is changed. The following example displays a message when a user switches view to determine if a user really wants to switch to the new view. If the user clicks No, the event is cancelled.

  Private Sub objPageWindowEvents_OnBeforeViewChange(ByVal _
        TargetView As FpPageViewMode, Cancel As Boolean)

    If (MsgBox("You have chosen to switch the current " & _
            "view. Are you sure you want to do that?", _
            vbYesNo, "Close page?") = vbNo) Then
        Cancel = True
    End If

End Sub

Working with Page Object Model Events

There are a variety of events for which you can create event handlers using the Page Object Model. For example, many of the HTML elements on a page allow for click and mouseover events. You can use the Page Object Model to handle these events at design time.

Note   For run-time event handling, you should use scripting inside your page.

The following example displays a message when a user clicks on the first hyperlink in the Preview page window.

  Private Function objAnchorEvent_onclick() As Boolean

    MsgBox "Hello, " & Application.UserName & ". You have " & _
        "just clicked a hyperlink. Click OK to continue."

End Function

To fire the above event, you must create an event handler class and initialize it (as shown above) so that when you click on a hyperlink in the FrontPage Preview window, it fires the onclick event and you receive the message.