20.1. Examining the Word Object Model

You don't need to understand how the Word object model fits together in order to work with VBA in Word, but most people find having a general idea of the object model helpful. To see the object model, follow these steps:

  1. Launch or activate Word, and then press Alt+F11 to launch or activate the Visual Basic Editor.

  2. Choose Help Microsoft Visual Basic Help MSDN on the Web.

  3. In the Live Search textbox, type Word 2007 object model map .

  4. In the list of search result links, click Word Application Object Model Map [Word 2007 Developer Reference]. Or you can enter this URL in the address field of your Internet browser: http://msdn2.microsoft.com/en-us/library/bb288731.aspx. You'll see the object model in your Internet browser (see Figure 20.1).

At this point you might want to add this web page to your Favorites, Bookmarks, or Links. This page is your gateway into the object model.

Figure 20.1. Microsoft Visual Basic Help provides a schematic of the Word object model. You can click an object to see details about it.

You can see the object model map in the regular (offline) Help screen, but it's a convoluted process getting to it. You might see a link to "Word object model maps" in the Help system, but the link is currently dead. More important, the online (MSDN) version of the map includes some useful features, such as the left pane table of contents and greater flexibility in general. (True, you can click the purple book icon on the Visual Basic Editor's Help window's toolbar to display a table of contents in a left pane, but in general the online help system is not only superior and can also be updated more easily.)

When you look at the Word object model on your screen, you'll see that some of the boxes are shaded yellow, while the others are shaded blue. The yellow boxes are collection objects, while the blue objects are not collections. Boxes with red stars are objects or members new in Office 2007.

Click a box to see the help screen for the object, often including useful code examples you can study or copy into your own VBA procedures.

Figure 20.2 shows part of the Help screen for the RecentFiles collection, which contains details of the files in the Most Recently Used list (the list that appears when you click the Office button).

A Shortcut: Understanding Creatable Objects

Like most VBA-enabled applications, Word has a number of creatable objects . This merely means that you don't have to employ the full qualification. In other words, you don't need to add the Application object in your code. For example, the Documents collection object is creatable, so you can omit its parent, the Application object, when using the collection in code, like this:

Dim x As Integer
x = Documents.Count
MsgBox x

However you can, if you wish, use the longer version:

x = Application.Documents.Count

The following are typically the most useful of these creatable objects:

  • The ActiveDocument object returns a Document object that represents the active document.

  • The ActiveWindow object returns a Window object that represents the active window.

  • The Documents collection contains the Document objects, each of which represents an open document.

  • The Options object represents Word options and document options, including most of the options that appear in the Options dialog box.

  • The Selection object represents the selection in the active document. Selection represents the selection (containing text or other objects) or collapsed selection (containing nothing — merely the blinking insertion point) in the document.

  • The Windows collection contains the Window objects that represent all open windows.


Figure 20.2. The Help screen for an individual object or collection includes useful code examples showing how to use the object in your programming.

The following sections show you how to work with some of these objects, starting with the Documents collection and the Document object. You'll see how to use the ActiveWindow object and the Windows collection in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.174