Special Edition Using Microsoft Office FrontPage 2003

It is often useful to write code that will run when the FrontPage application performs some type of action, such as opening a Web site. The Application object model provides several events that allow you to do this.

Suppose that you wanted to write a macro displaying a message when a Web site is opened that tells you the type of Web server that the Web site is running on and what version of the FrontPage Server Extensions the server is running. To implement this, you will need to do the following:

  • Create a new class module.

  • Declare a public variable to hold a reference to the FrontPage application.

  • Write code that will determine the version of the Web server and the FrontPage Server Extensions when a Web site is opened.

  • Intercept the FrontPage Application object event.

Create a New Class Module

In order to write code that runs when FrontPage opens a Web site, we must create a class module. A class module is just like a module except that a class module defines a class. (See the section "Class Module" earlier in this chapter for more information.) Insert a new class module into your macro project by right-clicking inside of the Project Explorer and selecting Insert, Class Module. Change the Name property of the class module to ServerInfo.

The Name property of a class module is very significant. You use the code in a class module by creating a new object from that class. (This new object is known as an instance of the class.) When you do that, the type of the instance is the name of the class module. In other words, when you create a new instance of the object defined in your new class module, that new instance is a ServerInfo object.

Declaring the FrontPage Application Variable

Now that you have a new class module, you need to declare a variable that will represent the FrontPage Application object. You'll need to do this using the WithEvents keyword. The WithEvents keyword exposes the events to you so that you can write code that will run when those events occur.

Double-click the ServerInfo class and enter the following code in the code window:

Public WithEvents fpApp As FrontPage.Application

The Public keyword allows you to access the fpApp variable from anywhere in your macro. After you have declared this variable, you need to add code that will set that variable equal to the current instance of FrontPage. You want that code to run as soon as your ServerInfo object is created. Class_Initialize is the event that gets triggered when a new instance of your class is created, so that's where we'll enter that code.

At the top of the ServerInfo code window, you will see two dropdowns; the Object dropdown on the left and the Procedure dropdown on the right. Select ServerInfo in the Object dropdown, and then select Initialize in the Procedure dropdown. This creates an empty code block for the Class_Initialize event. Click inside that event and enter the following code:

Set fpApp = Application

Now your fpApp variable contains a reference to the current instance of FrontPage.

Writing Code to Display Server Info

Now you need to write code that runs when a Web site is opened that displays the type of Web server and the version of the FrontPage Server Extensions. The event that will contain this code is the OnWebOpen event of the fpApp object you created.

Select fpApp from the Object dropdown and select OnWebOpen from the right dropdown. This inserts a block of code for the OnWebOpen event. Click inside that code and enter the following code:

Dim sWebServer As String Dim sExtensionsVersion As String sWebServer = pWeb.Properties("vti_httpdversion") sExtensionsVersion = pWeb.Properties("vti_extenderversion") MsgBox "Web server:" & vbTab & sWebServer & vbTab & vbCrLf & _ "FPSE Version:" & vbTab & sExtensionsVersion, _ vbInformation + vbOKOnly, "Server Information"

This code introduces a new property of the WebEx object; the Properties property. The Properties property contains a collection of properties that describe the Web site. In this particular case, we are concerned with the vti_httpdversion item, which returns the version of the Web server, and the vti_extenderversion, which returns the version of the FrontPage Server Extensions. (For a complete list of properties and their values, go to http://webproperties.frontpagelink.com.)

Intercept the FrontPage Application Event

The last step in writing your macro is to add the code that will intercept the OnWebOpen event. You'll need to add this code to Module1 as a Public sub procedure just as you did with the StartHTMLOptimizer macro.

Double-click Module1 to open the code window and enter the following code at the top of the code window before any other code:

Public cServerInfo As ServerInfo

This is a public declaration, so it must be outside of any sub procedures or functions. By making it public, you are making it available to your entire macro for the life of your macro.

Now you need to create a new instance of the ServerInfo class and assign the value of the cServerInfo variable to that instance. Place the insertion point at the bottom of the code window and enter the following code:

Public Sub InterceptFPEvent() Set cServerInfo = New ServerInfo End Sub

This code will be the name of the macro that we will run from the Macro dialog in FrontPage. It creates a new instance of the ServerInfo class, which will run the Class_Initialize event of the ServerInfo class and intercept the FrontPage application events.

To test your macro, switch back to FrontPage and select Tools, Macros, Macro. Select the InterceptFPEvent macro and click Run to run it. It will seem as though nothing happens, but you have actually instantiated a new instance of your ServerInfo class, and it is now waiting for you to open a Web site. Open a Web site in FrontPage, and you should see a dialog much like the one in Figure 30.8.

Figure 30.8. The Server Information dialog indicating server version and FrontPage Server Extensions version.

Категории