Special Edition Using Microsoft Office FrontPage 2003

Now that you have a general understanding of the pieces that make up a VBA macro, let's take a look at how we can use a macro to automate FrontPage.

When you write macros, you access parts of the application you're developing against using a predefined set of classes, properties, and methods called an object model. FrontPage actually has three different object models that you can use; the Application object model, the Web object model, and the Page object model. As you might have guessed, you access the FrontPage application using the Application object model, you access Web sites with the Web object model, and you access Web pages and their content using the Page object model.

The Application Object Model

The top-level object in the Application object model is the Application object. The Application object represents the current instance of the FrontPage application itself.

Suppose that we want to display the version of FrontPage that we are running. We can do that using the following code:

MsgBox Application.Version

This code displays the Version property of the Application object in a message box.

The Web Object Model

At the core of the Web object model is the WebEx object. A WebEx object represents a FrontPage Web site. You can get a reference to a WebEx object as follows:

1 Dim oWeb As WebEx 2 Set oWeb = Webs.Open("http://localhost", , fpOpenInWindow)

Line numbers are included for reference only and are not part of the code. In the first line we declare a variable called oWeb and indicate that it will contain a WebEx object. We then open the Web site located at http://localhost and set the oWeb object equal to that Web site. After this code executes, oWeb will contain a reference to the root Web on the local machine and we can begin accessing the properties of that site and calling its methods.

For example, suppose that we want to publish the root Web of the local machine to http://www.mysite.com. We can do that using the code in Listing 30.1.

Listing 30.1 Publishing to Root Web

1 Dim oWeb As WebEx 2 Set oWeb = Webs.Open("http://localhost", , fpOpenInWindow) 3 oWeb.Publish "http://www.mysite.com", fpPublishAddToExistingWeb

As you can see, writing code to automate functionality in FrontPage is easy once you get the hang of it.

The Object Browser

The Object Browser is an excellent tool for viewing all the objects, properties, and methods of FrontPage's object models. You can open the Object Browser by selecting View, Object Browser (or press F2).

As seen in Figure 30.3, the Object Browser provides a comprehensive list of all the objects, properties, and methods in your macro project. When a class is selected in the Classes frame on the left, the Members pane on the right displays all the properties and methods of that class. By right-clicking on any class, property, or method and choosing Help from the menu, you have quick access to the documentation for that object. You can also select the object and press F1 to get help.

Figure 30.3. The Object Browser provides quick and easy access to all the objects in your project. Help is easily accessible by selecting an object and pressing F1.

The Object Browser is fully searchable. Suppose that you want to find the documentation on the Open method of a FrontPage WebEx object. Simply enter "open" in the search box and click the Search button the button that looks like a pair of binoculars. Using this method, you will often not only find the specific item you need, but you will also likely see other objects of interest that will help you to increase your knowledge of the FrontPage object models.

Experiment with the Object Browser for a while. Find an interesting object in the FrontPage object model and try to write a macro that uses that object. You'll find that taking this approach will dramatically increase your knowledge, and you'll learn skills that you can draw on for many years to come.

The Page Object Model

Now that we've got a WebEx object, we can easily access that Web site's files by using the WebFile object. If, for example, we wanted to open the page called default.htm in the root folder of the Web site, we would do it by adding the code in Listing 30.2 to the code we entered in Listing 30.1.

Listing 30.2 Opening Page from Root

1 Dim oFile As WebFile 2 Set oFile = oWeb.RootFolder.Files("default.htm") 3 oFile.Open

Категории