Chapter 20. Understanding the Word Object Model and Key Objects

This chapter shows you how to start coming to grips with the Word object model and the architecture underlying Word, and how to perform common tasks with the most frequently useful Word objects. These objects include the Documents collection and the Document object, the Selection object, Range objects, and the Options object.

In this chapter you will learn to do the following:

  • Understand the Word object model

  • Understand Word's creatable objects

  • Work with the Documents collection and the Document object

  • Work with the Selection object

  • Create and use ranges

  • Manipulate options

Examining the Word Object Model

You don't need to understand how the entire 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. Some VBA programming involves managing objects, and for this the Help system's code examples are often invaluable. To see Word's object model reference, follow these steps:

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

  2. Press F1. If you see a message in the Help dialog box that says "loading" or "downloading," be patient. But if nothing appears for more than 30 seconds, close the dialog box and try pressing F1 again (this help system can be a bit quirky). You should eventually see a dialog box similar to Figure 20.1

  3. If necessary, click the book icon (second from the right in the row of icons in the Word Help dialog box). This opens the table of contents within the Help dialog box.

  4. In the table of contents, click Word Object Model Reference. You'll now see the whole collection of syntax specifications, useful descriptions, and code examples, as shown in Figure 20.2.

The Word Help Dialog Box

Figure 20.1. The Word Help Dialog Box

The entries in the Word Object Model Reference will help you write your own VBA code.

Figure 20.2. The entries in the Word Object Model Reference will help you write your own VBA code.

The following sections show you how to work with some of the most useful Word 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.

Working with the Documents Collection and the Document Object

In many of your Word procedures, you'll likely work with documents: creating new documents, saving documents, opening existing documents, closing documents, and printing documents. To do so, you work with the Documents collection, which contains a Document object for each open document in Word.

Creating a Document

To create a new file, use the Add method of the Documents collection. The syntax is as follows:

expression.Add Template, NewTemplate, DocumentType, Visible

Here, expression is a required expression that returns a Documents collection. Typically, you'll want to use the Documents collection itself (Documents.Add).

Template is an optional Variant argument that specifies the template on which to base the new document. If you omit Template, Word uses the Normal template (as if you'd clicked the File tab on the Ribbon, then clicked the New button to open a blank document). So you need specify a Template argument only when you want to base the new document on a template other than the default Normal.dotm.

NewTemplate is an optional Variant argument that you can set to True to create a template file (.dotx) rather than a document. NewTemplate is set to False by default, so you can safely omit this argument unless you're creating a template.

DocumentType is an optional Variant argument that you can use to specify the type of document to create: wdNewBlankDocument (the default), wdNewEmailMessage, wdNewFrameset (for a frameset), or wdNewWebPage.

Visible is an optional Variant argument that you can set to False to have the document created in a window that isn't visible. The default setting is True, making the document window visible.

There are two ways to create a document:

Creating a document based on Normal.dotm

The following statement creates a new document based on the Normal.dotm global template:

Documents.Add
Creating a document based on a template

The following statement creates a new document based on the template named Company Report.dotm stored in the network folder designated \serverpublic emplates:

Documents.Add Template:= "\serverpublic	emplatesCompany Report.dotm"

Creating a Template

The following statements declare a new object variable named myTemplate of the Document class, create a new template based on the template named Fancy.dotx (stored in one of the default template folders), and assign it to myTemplate:

Dim myTemplate As Document

Set myTemplate = Documents.Add(Template:="c:program files (x86)Microsoft OfficeOffice141033Quickstylesfancy.dotx", _
    NewTemplate:=True, Visible:=True)

In this example, the path to the template is specified because this template is not in one of the default template folders. The result is a new .dotx file, based on the Fancy.dotx template.

Saving a Document

Just as when a user is saving a newly created document via the keyboard and mouse, when executing VBA code you must specify a filename and path the first time you save a document. After that, you can save it under the same name or specify a different name or format. This is the difference between the Save and Save As options.

Saving a File for the First Time or as a Different File

To save a file for the first time, or to save a file under a different name or in a different format, use the SaveAs method. The syntax is as follows:

expression.SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles,
WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat,
SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions,
LineEnding, AddBiDiMarks, CompatibilityMode

Here, expression is an expression that returns a Document object. For example, you might use the ActiveDocument object or an object in the Documents collection.

FileName is an optional Variant argument that specifies the name for the document. If you omit FileName, VBA uses the current folder and the default filename of Docn.docx( or .docm) or a document and Dotn.dotx (or .dotm) for a template, where n is the next available number (for example, Doc5.docx for a macro-free document or Dot2.dotm for a macro-enabled template).

FileFormat is an optional Variant argument that specifies the format in which to save the document. Table 20.1 lists the wdSaveFormat constants for specifying commonly used formats.

Table 20.1. WdSaveFormat constants

Constant

Saves Document As

wdFormatDocument

A Word document

wdFormatDOSText

A DOS text file

wdFormatDOSTextLineBreaks

A DOS text file with layout

wdFormatEncodedText

A text file with encoding

wdFormatFilteredHTML

A filtered HTML file (Word 2003 and XP only)

wdFormatFlatXML

An unindexed XML document

wdFormatFlatXMLMacroEnabled

An unindexed XML document with macro capability

wdFormatFlatXMLTemplate

An unindexed XML template

wdFormatFlatXMLTemplateMacroEnabled

An unindexed XML template with macro capability

wdFormatHTML

An HTML file

wdFormatOpenDocumentText

An XML file format developed by Sun Microsystems

wdFormatRTF

A Rich Text Format file

wdFormatTemplate

A Word template

wdFormatText

A text file (plain ASCII)

wdFormatTextLineBreaks

A text file with line breaks

wdFormatUnicodeText

A text file with Unicode characters

wdFormatWebArchive

A web archive file

wdFormatXML

An XML file (Word 2003 only)

wdFormatDocument97

Microsoft Word 97 document format

wdFormatDocumentDefault

Word 2010 default document file (.docx)

wdFormatPDF

PDF format

wdFormatTemplate97

Word 97 template format

wdFormatXMLDocument

XML document format

wdFormatXMLDocumentMacroEnabled

XML document format with macros enabled

wdFormatXMLTemplate

XML template format

wdFormatXMLTemplateMacroEnabled

XML template format with macros enabled

wdFormatXPS

XPS format

The following statement saves the active document as a filtered HTML file under the name Example.html in the current folder:

ActiveDocument.SaveAs FileName:="Example.html", _
    FileFormat:=wdFormatFilteredHTML

After you run this example code, use Windows Explorer to locate this new Example.html file and click on it. It will open in Internet Explorer as if it were a web page, because it's stored using the HTML format (if Internet Explorer is the default application in which .html files are opened). Or take a look at it in Notepad if you want to see the full horror of HTML markup.

AddToRecentFiles is an optional Variant argument that you can set to True to have Word add the document to the list of recently used files displayed when you click the File tab on the Ribbon and then click Recent. (Often, when experimenting with documents in procedures, you'll want to avoid listing them on the Most Recently Used list, leaving the user's previous list of recent files undisturbed.)

To protect the document as you save it, you can use four different protection features:

  • LockComments is an optional Variant argument that you can set to True to lock the document so that reviewers can enter comments but can't change the text of the document.

  • Password is an optional Variant argument that you can use to set a password required before opening the document.

  • WritePassword is an optional Variant argument that you can use to set a password required before saving changes to the document.

  • ReadOnlyRecommended is an optional Variant argument that you can set to True to have Word recommend that the user open the document as read-only.

Finally, there are the following optional arguments you'll use infrequently, if ever:

  • EmbedTrueTypeFonts is an optional Variant argument that you can set to True to save TrueType fonts with the document. (This is a good idea only if you're distributing the document to someone you know doesn't have the TrueType fonts installed to view the document correctly.)

  • SaveNativePictureFormat is an optional Variant argument that you can set to True to have graphics imported from another platform saved as Windows graphics.

  • SaveFormsData is an optional Variant argument that you can set to True to save the data entered in a form as a data record (as opposed to saving the whole form, including its static text).

  • SaveAsAOCELetter is an optional Variant argument that you can set to True to save the document as an AOCE (Apple Open Collaboration Environment) letter (a mailing format for routing documents).

  • Encoding is an optional Variant argument for using a different code page than the system code page. For example, you might need to save a document using a Cyrillic code page.

  • InsertLineBreaks is an optional Variant argument that you can set to True when saving a document as a text file to make Word insert a line break at the end of each line of text.

  • AllowSubstitutions is an optional Variant argument that you can set to True when saving a document as a text file to make Word substitute some symbol characters with similar text. For example, Word substitutes (TM) for a trademark symbol (™).

  • LineEnding is an optional Variant argument that you can use when saving a document as a text file to control how Word marks line breaks and paragraph breaks.

  • AddBiDiMarks is an optional Variant argument that you can set to True to make Word add control characters to the file to maintain bidirectional layout.

Usually, when saving a file for the first time, you'll need to specify only its name and path; if you want to save it in a format other than a Word document, specify that too. The following statement saves the active document under the name Beehives.docx in the folder \serverProductsField:

ActiveDocument.SaveAs _
    "\serverProductsFieldBeehives.docx"

Saving a Document That Has Already Been Saved

Once a document has been saved, you can save it under the same name by using the Save method. For a Document object, the Save method takes no arguments (all the document's current formats are saved unchanged). For example, the following statement saves the document named Recipe01.docx:

Documents("Recipe01.docx").Save

Saving All Open Documents

To save all open documents, use the Save method with the Documents collection. The syntax is as follows:

expression.Save(NoPrompt, OriginalFormat)

Here, expression is an expression that returns a Documents collection. Often, you'll use the Documents collection itself.

NoPrompt is an optional Variant argument that you can set to True to make Word save all open documents containing unsaved changes and any attached templates containing unsaved changes without prompting the user. The default setting is False, which causes Word to prompt the user whether to save each document and template. Even if you set NoPrompt to True, Word will prompt the user to save changes to Normal.dotm if the Prompt Before Saving Normal Template check box is selected in the Save section of the Advanced tab of the Options dialog box.

OriginalFormat is an optional Variant argument that you can set to wdOriginalDocumentFormat to save the documents in their original formats, wdWordDocument to force each document to be saved as a Word document, or wdPromptUserX to prompt the user about which format to use.

For example, the following statement saves all open documents and templates without prompting the user:

Documents.Save NoPrompt:=True

Checking Whether a Document Contains Unsaved Changes

To find out whether a document contains unsaved changes, check its Saved property. Saved is a read/write Boolean property that returns False if the document contains unsaved changes and True if it does not. A new document contains no unsaved changes, even though it has never been saved.

Opening a Document

To open a document, use the Open method with the appropriate Document object. The syntax for the Open method is as follows:

expression.Open FileName, ConfirmConversions, ReadOnly,
AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument,  WritePasswordTemplate,
Format, Encoding, Visible, OpenConflictDocument,
OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform

The arguments are as follows:

  • expression is a required expression that returns a Documents collection. Usually, you'll want to use the Documents collection itself.

  • FileName is a required Variant argument specifying the name (and path, if necessary) of the document to open.

  • ConfirmConversions is an optional Variant argument that you can set to True to have Word display the Convert File dialog box if the file is in a format other than Word.

  • ReadOnly is an optional Variant argument that you can set to True to open the document as read-only.

  • AddToRecentFiles is an optional Variant argument that you can set to True to have Word add the filename to the list of recently used files at the foot of the File menu.

  • PasswordDocument is an optional Variant argument that you can use to set a password for opening the document.

  • PasswordTemplate is an optional Variant argument that you can use to set a password for opening the template.

  • Revert is an optional Variant argument that specifies what Word should do if the FileName supplied matches a file that's already open. By default (that is, if you don't include the Revert argument), Revert is set to False, which means that Word activates the open instance of the document and doesn't open the saved instance. You can set Revert to True to have Word open the saved instance of the document and discard any changes to the open instance.

  • WritePasswordDocument is an optional Variant argument that indicates the password for saving changes to the document.

  • WritePasswordTemplate is an optional Variant argument that specifies the password for saving changes to the template.

  • Format is an optional Variant argument that you can use to specify the file converter with which to open the document. Table 20.2 lists the WdOpenFormat constants you can use specify the file converter.

  • Encoding is an optional Variant argument specifying the document encoding (the code page or the character set) for Word to use when opening the document.

  • Visible is an optional Variant argument that you can set to False to have Word open the document in a window that isn't visible. (The default setting is True, specifying a visible window.)

  • OpenConflictDocument is an optional Variant that specifies if a conflict file should be opened for a document with an offline conflict.

  • OpenAndRepair is an optional Variant that, when True, repairs the document to prevent corruption.

  • DocumentDirection is an optional WdDocument Direction variable type, indicating the horizontal flow of text in the document. The default is wdLeftToRight.

  • NoEncodingDialog is an optional Variant that defaults to False. But if it's set to True, the Encoding dialog box is not displayed when Word cannot recognize text encoding.

  • XMLTransform is mysterious. The only explanation I could find is in MSDN and it merely says, "Specifies a transform to use." So your guess is as good as mine about what this option accomplishes.

Table 20.2. WdOpenFormat constants for opening a document

Constant

Effect

wdOpenFormatAllWord

Word opens the document in any recognized Word format as a Word document.

wdOpenFormatAuto

Word chooses a converter automatically. This is the default setting.

wdOpenFormatDocument

Word opens the document as a Word document.

wdOpenFormatEncodedText

Word opens the document as a text file with encoding.

wdFormatOpenDocumentText

Word opens the document in an XML file format developed by Sun Microsystems.

wdOpenFormatRTF

Word opens the document as a Rich Text format file.

wdOpenFormatTemplate

Word opens the document as a template.

wdOpenFormatText

Word opens the document as a text file.

wdOpenFormatUnicodeText

Word opens the document as a Unicode text file.

wdOpenFormatWebPages

Word opens the document as a web page.

wdOpenFormatXML

Word opens the document in XML format.

wdOpenFormatAllWordTemplates

Word template.

wdOpenFormatDocument97

Microsoft Word 97 document format.

wdOpenFormatTemplate97

Word 97 template format.

wdOpenFormatXMLDocument

XML document format.

wdOpenFormatXMLDocumentSerialized

Word opens an XML document by reconstructing the original document structure from a one-dimensional stream of bits.

wdOpenFormatXMLDocumentMacroEnabled

XML document format with macros enabled.

wdOpenFormatXMLDocumentMacro-EnabledSerialized

Word opens an XML document with macros enabled by reconstructing the original document from a one-dimensional stream of bits.

wdOpenFormatXMLTemplate

XML template format.

wdOpenFormatXMLTemplateMacroEnabled

XML template format with macros enabled.

wdOpenFormatXMLTemplateMacro-EnabledSerialized

Word opens an XML template with macros enabled by reconstructing the original document from a one-dimensional stream of bits.

wdOpenFormatXMLTemplateSerialized

Word opens an XML template by reconstructing the original document from a one-dimensional stream of bits.

The following statement opens the document Times.docx found in the C:My Documents folder:

Documents.Open "C:My DocumentsTimes.docx"

The following statement opens the file notes.docm in the folder C: emp as read-only and adds it to the list of most recently used files (the list you see when you click the File tab on the Ribbon, then click Recent):

Documents.Open "C:	emp
otes.docm", ReadOnly:=True, _
    AddToRecentFiles:=True

Closing a Document

To close a document, use the Close method with the application Document object. The syntax is as follows:

expression.Close(SaveChanges, OriginalFormat, RouteDocument)

Here, expression is a required expression that returns a Document object or a Documents collection. Typically you use the ActiveDocument object or, to close all documents, the Documents collection object.

SaveChanges is an optional Variant argument you can use to specify how to handle unsaved changes. Use wdDoNotSaveChanges to discard changes, wdPromptToSaveChanges to have Word prompt the user to save changes, or wdSaveChanges to save changes without prompting.

OriginalFormat is an optional Variant argument you can use to specify the save format for the document. Use wdOriginalDocumentFormat to have Word use the original document format, wdPromptUser to have Word prompt the user to choose a format, or wdWordDocument to use the Word document format.

RouteDocument is an optional Variant argument that you can set to True to route a document that has a routing slip attached.

For example, the following statement closes the active document without saving changes:

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

The following statement closes all open documents (but not the Word application itself) and saves changes automatically:

Documents.Close SaveChanges:=wdSaveChanges

Changing a Document's Template

To change the template attached to a document, set the AttachedTemplate property of the Document object you want to affect to the path and name of the appropriate template. For example, the following statement attaches the template named SalesMarket02.dotm to the active document. In this example, the template is assumed to be stored in one of the Word templates folders, so the path need not be specified:

ActiveDocument.AttachedTemplate = "SalesMarket02.dotm"

Printing a Document

To print a document, use the PrintOut method for the appropriate Document object. The syntax for the PrintOut method is as follows:

expression.PrintOut(Background, Append, Range, OutputFileName, From, To, Item,
Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX,
ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth,
PrintZoomPaperHeight)

These are the components of the PrintOut method:

  • expression is a required expression specifying an Application, Document, or Window object. Usually, you'll print a Document object such as ActiveDocument.

  • Background is an optional Variant argument that you can set to True to have Word print the document in the background, allowing the procedure to continue running.

  • Append is an optional Variant argument that you can set to True to append the document being printed to file to the print file specified.

  • Range is an optional Variant argument specifying the selection or range of pages to print: wdPrintAllDocument (0, the default), wdPrintCurrentPage (2), wdPrintFromTo (3; use the From and To arguments to specify the pages), wdPrintRangeOfPages (4), or wdPrintSelection (1).

  • OutputFileName is an optional Variant argument used to specify the name for the output file when printing to file.

  • From is an optional Variant argument used to specify the starting page number when printing a range of pages.

  • To is an optional Variant argument used to specify the ending page number when printing a range of pages.

  • Item is an optional Variant argument used to specify the item to print: wdPrintAutoTextEntries (4), wdPrintComments (2), wdPrintDocumentContent (0, the default), wdPrintKeyAssignments (5, shortcut key assignments for the document or its template), wdPrintProperties (1), or wdPrintStyles (3).

  • Copies is an optional Variant argument used to specify the number of copies to print. (If you omit Copies, Word prints one copy.)

  • Pages is an optional Variant argument used to specify the pages to print—for example, 1, 11-21, 31.

  • PageType is an optional Variant argument used to specify whether to print all pages (wdPrintAllPages, 0, the default), odd pages (wdPrintOddPagesOnly, 1), or even pages (wdPrintEvenPagesOnly, 2).

  • PrintToFile is an optional Variant argument that you can set to True to direct the output of the print operation to a file.

  • Collate is an optional Variant argument used when printing multiple copies of a document to specify whether to collate the pages (True) or not (False).

  • FileName is an optional Variant argument used to specify the filename and path of the document to print. (If you omit FileName, VBA prints the active document.)

  • ActivePrinterMacGX is an optional Variant argument used on the Macintosh to specify the printer if QuickDraw GX is installed.

  • ManualDuplexPrint is an optional Variant argument that you set to True for two-sided printing on a printer that doesn't have duplex capabilities. When ManualDuplexPrint is True, you can use the PrintOddPagesInAscendingOrder property or the PrintEvenPagesInAscendingOrder property of the Options object to print odd or even pages in ascending order to create a manual duplex effect (reloading the odd-page-printed paper into the printer the other way up to print the even pages). The ManualDuplexPrint argument is available only in some languages.

  • PrintZoomColumn and PrintZoomRow are optional Variant arguments that you use to specify the number of pages to print on a page horizontally (PrintZoomColumn) and vertically (PrintZoomRow). Each property can be 1, 2, or 4.

  • PrintZoomPaperWidth is an optional Variant argument that you can use to specify the width (measured in twips) to which to scale printed pages.

  • PrintZoomPaperHeight is an optional Variant argument that you can use to specify the height (measured in twips) to which to scale printed pages.

For example, the following statement prints three collated copies of the active document in the background:

ActiveDocument.PrintOut Background:=True, Copies:=3, Collate:=True

The following statement prints pages 2 through 5 of the active document:

ActiveDocument.PrintOut Range:=wdPrintFromTo, From:=2, To:=5

The following statement prints the active document at two virtual pages per sheet of paper:

ActiveDocument.PrintOut PrintZoomColumn:=2, PrintZoomRow:=1

Working with the ActiveDocument Object

The ActiveDocument object returns a Document object that represents the current document you're working with—whichever document has the focus in the Word window. The ActiveDocument object behaves like a Document object, but watch out for two possible problems when working with it.

You may have problems locating information about the ActiveDocument object in the Help system. It's actually a property of the Application object, so its status as an actual object is somewhat iffy. Object taxonomy is an evolving clerical system and, as you see, remains imperfect.

To find the ActiveDocument object in the Help system, MSDN system, or VBA Editor Object Browser, you need to first locate the Application object, then look at its properties (or members). Just remember, ActiveDocument is found only under the Application object. It's a clerical error. It's as if you were looking for California in a geography book's index, but the index is wacky because you find most states listed under their own names (Hawaii is under the Hs, for example). But for some reason, California is not listed in the Cs. You're puzzled. It's a big, important state. Then you stumble upon the solution: In this bizarre index, California is only found under the entry for United States.

The second oddity about the ActiveDocument "property" is that it can be evanescent. The first problem is that if there's no document open in Word, there's no ActiveDocument object, and any code that tries to work with the ActiveDocument object returns an error. You should remember to check the Count property of the Documents collection to make sure there's a document open (Count will be at least 1) before attempting to use the ActiveDocument in your code. Here's an example that tests to see if there is an open document:

If Documents.Count = 0 Then
    If MsgBox("No document is open." & vbCr & vbCr & _
        "Do you want to create a new blank document?", _
        vbYesNo + vbExclamation, "No Document Is Open") = vbYes Then
Documents.Add
    Else
        End
    End If
End If

The second problem relating to this evanescence is that a different document may be active than your code assumes is active. This problem tends to occur when a procedure starts with the active document and then creates a new document to work in; the new document becomes the active document, and from this point on, confusion may result.

If you know the name of the document that should be active, you can check to see if the name of the active document matches it, to verify that you'll be working with the right document.

If there's any doubt about which document you're working with, declare a Document object variable and employ that object variable in your code rather than the ActiveDocument object.

For example, the following statements declare a Document object and assign the ActiveDocument object to it so that subsequent code can work with the Document object:

Dim myDocument As Document
Set myDocument = ActiveDocument
With myDocument
    'actions here
End With

Or if you know the name of the document you want to work with:

Dim myDocument As Document
Set myDocument = ActiveDocument
If myDocument.Name = "CorrectFile.docx" Then
    'actions here
End If

Working with the Selection Object

To write code that works with only part of a document (a word, paragraph, or whatever), you can access that zone in three ways:

  • By using the Selection object

  • By directly accessing the object that you want to affect

  • By defining a range that encompasses the object

Using the Selection object is analogous to working interactively with Word and works well with procedures that require the user to select an object or position the insertion point to denote what the procedure should affect.

Using the Selection object is also effective when you're learning to use VBA with Word, because many actions that you record using the Macro Recorder use the Selection object.

The Selection object represents the current selection in the active document in Word. The selection can be very small (collapsed to the insertion point, the blinking cursor), in which case nothing is selected. Or it can contain one or more objects—one or more characters, one or more words, one or more paragraphs, a graphic, a table, the entire document. Or the selection can be a combination of these objects. Whatever's selected.

Even if the selection is collapsed to an insertion point, you can use it to refer to objects outside the selection. For example, Selection.Paragraphs(1).Range.Words(10).Text returns the 10th word in the paragraph in which the insertion point is positioned or the 10th word in the first paragraph of the one or more paragraphs that are selected.

Checking the Type of Selection

Word recognizes nine different types of selections. When you're working in the active document, you'll often need to check what kind of selection is active so that you know whether you're dealing with a block of selected text, no selection (insertion point), or a special type of selection such as a table or a graphic.

Depending on the current selection, you may not be able to take certain actions in a procedure, and you may not want to take others. You can't, for example, insert a table row into an ordinary text paragraph.

Table 20.3 lists the types of selections that Word differentiates.

Table 20.3. Selection types in Word

wdSelectionType constant

Value

Meaning

wdNoSelection

0

There's no selection. (This state seems impossible to achieve. You'd think it'd be when no document is open, but then Selection statements return runtime error 91. Stay tuned...)

wdSelectionIP

1

The selection is collapsed to a plain insertion point—nothing is selected. But the insertion cursor is blinking as usual.

wdSelectionNormal

2

A "normal" selection, such as a selected word or sentence.

wdSelectionFrame

3

A frame is selected.

wdSelectionColumn

4

A column or part of a column (two or more cells in a column or one cell in each of two or more columns) is selected.

wdSelectionRow

5

A full row in a table is selected.

wdSelectionBlock

6

A block is selected (a vertical part of one or more paragraphs, selected by holding down the Alt key and dragging with the mouse or by using column-extend mode).

wdSelectionInlineShape

7

An inline shape or graphic (a shape or graphic that's in the text layer rather than floating over it) is selected.

wdSelectionShape

8

A Shape object is selected. (A text box counts as a Shape object.)

To find out what type of selection you currently have, look at the Type property of the Selection object. The following statements check that the current selection is merely an insertion point before inserting a text literal. The text will not be inserted if the user has dragged to select, for example, some characters, a word, or a paragraph:

If Selection.Type = wdSelectionIP Then
    Selection.TypeText "This is inserted."
End If

Checking the Story Type of the Selection

Beyond the type of selection, you'll sometimes need to find out which "story" the selection is in—the main text story, the comments story, the primary header story, and so on. Microsoft uses the word story instead of zone, type, or other terms to mean a distinct type of content.

Checking the story can help you avoid problems, such as trying to perform actions in a header or footer that Word supports only in a main text story.

The story is the zone of the document within which the current selection is located. So, most of the time the story is the main text story (wdMainTextStory). That's the document and the items within it. But alternative "stories" are things like footnotes, frames, headers, and footers—as you can see in Table 20.4.

Table 20.4. Word story types

wdStoryType Constant

Value

Meaning

wdMainTextStory

1

Main (body) text of the document

wdCommentsStory

4

Comments section

wdEndnotesStory

3

Endnotes section

wdFootnotesStory

2

Footnotes section

wdTextFrameStory

5

Text in frames

wdPrimaryFooterStory

9

Main footer

wdEvenPagesFooterStory

8

Even-page footer

wdFirstPageFooterStory

11

First-page footer

wdPrimaryHeaderStory

7

Main header

wdEvenPagesHeaderStory

6

Even-page header

wdFirstPageHeaderStory

10

First-page header

wdFootnoteSeparatorStory

12

Footnote separator

wdFootnoteContinuationSeparatorStory

13

Footnote continuation separator

wdFootnoteContinuationNoticeStory

14

Footnote continuation notice

wdEndnoteSeparatorStory

15

Endnote separator

wdEndnoteContinuationSeparatorStory

16

Endnote continuation separator

wdEndnoteContinuationNoticeStory

17

Endnote continuation notice

You may notice another whimsical, enigmatic feature of Table 20.4. It starts the enumeration value with 1. Compare that to Table 20.3 which starts with 0. Inconsistencies like this make programming more challenging.

Table 20.4 lists the wdStoryType constants and the stories to which they correspond.

Here's a code example that displays a message box if the selection isn't in the main text of a document:

If Selection.StoryType <> wdMainTextStory Then
    MsgBox "This range is not in the main text."
End If

Getting Other Information about the Current Selection

To work effectively with the current selection, you'll often need to know what it contains and where it's positioned. To find out, use the Information property to learn the details you need. Table 20.5 lists the information available in the Information property.

For example:

If Selection.Information(wdCapsLock) = True Then
    MsgBox "The caps lock is ON."
End If

Sharp-eyed readers will notice a capricious inconsistency in this code. In the other code examples in this section, no parentheses were used around a constant, and the operator (= or <> or whatever) is placed between the property and the constant, as shown in this example:

Selection.Type = wdSelectionIP

But with the Information property, you do use parentheses, and you move the operator to the right of the constant:

Selection.Information(wdCapsLock) =

This syntax and punctuation irregularity is yet another of those exceptions to the rule. You should therefore remember that if the usual syntax produces an error message from the editor, try the other (parenthetical) version.

Table 20.5. Information available in the Information property

Constant Environment Information

Returns This Information

wdCapsLock

True if Caps Lock is on.

wdNumLock

True if Num Lock is on.

wdOverType

True if Overtype mode is on. (You can turn Overtype mode on and off by changing the Overtype property.)

wdRevisionMarking

True if Track Changes is on.

wdSelectionMode

A value that specifies the current selection mode: 0 indicates a normal selection, 1 indicates an extended selection (Extend mode is on), and 2 indicates a column selection.

wdZoomPercentage

The current zoom percentage.

Selection and Insertion Point Information

 

wdActiveEndAdjustedPageNumber

The number of the page containing the active end of the selection or range. This number reflects any change you make to the starting page number; wdActiveEndPageNumber, the alternative, doesn't.

wdActiveEndPageNumber

The number of the page containing the active end of the selection or range.

wdActiveEndSectionNumber

The number of the section containing the active end of the selection or range.

wdFirstCharacterColumnNumber

The character position of the first character in the selection or range. If the selection or range is collapsed to an insertion point, this constant returns the character number immediately to the right of the insertion point. (Note that this "column" is relative to the currently active left margin and doesn't have to be inside a table. This is the number that appears in the Col readout in the status bar.)

wdFirstCharacterLineNumber

In Print Layout view and Print Preview, this constant returns the line number of the first character in the selection. In nonlayout views (e.g., Normal view), it returns −1.

wdFrameIsSelected

True if the selection or range is a whole frame or text box.

wdHeaderFooterType

A value that specifies the type of header or footer containing the selection or range: −1 indicates that the selection or range isn't in a header or footer; 0 indicates an even page header; 1 indicates an odd page header in a document that has odd and even headers and the only header in a document that doesn't have odd and even headers; 2 indicates an even page footer; 3 indicates an odd page footer in a document that has odd and even footers and the only footer in a document that doesn't have odd and even headers; 4 indicates a first-page header; and 5 indicates a first-page footer.

wdHorizontalPositionRelativeToPage

The horizontal position of the selection or range—the distance from the left edge of the selection or range to the left edge of the page, measured in twips.

wdHorizontalPositionRelativeToTextBoundary

The horizontal position of the selection or range—the distance from the left edge of the selection or range to the text boundary enclosing it, measured in twips.

wdInCommentPane

True if the selection or range is in a comment pane.

wdInEndnote

True if the selection or range is an endnote (defined as appearing in the endnote pane in Normal view or in the endnote area in Print Layout view).

wdInFootnote

True if the selection or range is in a footnote (defined as appearing in the footnote pane in Normal view or in the footnote area in Print Layout view).

wdInFootnoteEndnotePane

True if the selection or range is in a footnote or endnote.

wdInHeaderFooter

True if the selection or range is in a header or footer (defined as appearing in the header or footer pane in Normal view or in the header or footer area in Print Layout view).

wdInMasterDocument

True if the selection or range is in a master document (a document containing at least one subdocument).

wdInWordMail

A value that specifies the WordMail location of the selection or range: 0 indicates that the selection or range isn't in a WordMail message; 1 indicates that it's in a WordMail message you're sending; 2 indicates that it's in a WordMail you've received.

wdNumberOfPagesInDocument

The number of pages in the document in which the selection or range appears.

wdReferenceOfType

A value that specifies where the selection is in relation to a footnote reference, endnote reference, or comment reference. −1 indicates the selection or range includes a reference. 0 indicates the selection or range isn't before a reference. 1 indicates the selection or range is before a footnote reference, 2 that it's before an endnote reference, and 3 that it's before a comment reference.

wdVerticalPositionRelativeToPage

The vertical position of the selection or range—the distance from the top edge of the selection to the top edge of the page, measured in twips.

wdVerticalPositionRelativeToTextBoundary

The vertical position of the selection or range—the distance from the top edge of the selection to the text boundary enclosing it, measured in twips.

Table Information

 

wdWithInTable

True if the selection is in a table.

wdStartOfRangeColumnNumber

The number of the table column containing the beginning of the selection or range.

wdEndOfRangeColumnNumber

The number of the table column containing the end of the selection or range.

wdStartOfRangeRowNumber

The number of the table row containing the beginning of the selection or range.

wdEndOfRangeRowNumber

The number of the table row containing the end of the selection or range.

wdAtEndOfRowMarker

True if the selection or range is at the end-of-row marker in a table (not the end-of-cell marker).

wdMaximumNumberOfColumns

The largest number of table columns in any row in the selection or range.

wdMaximumNumberOfRows

The largest number of table rows in the table in the selection or range.

Macintosh

 

wdInClipboard

Used with Microsoft Office Macintosh Edition

Inserting Text at, after, or before the Selection

You can insert text at the selection by using the TypeText method of the Selection object, insert text before the selection by using the InsertBefore method, or insert text after the selection by using the InsertAfter method.

The TypeText method inserts text if the selection is collapsed (merely the blinking cursor with nothing actually selected). But if something is selected, such as a word or phrase, that selection is replaced by the string when you execute TypeText. However, the InsertBefore and InsertAfter methods do not replace a selection. They merely insert the new string.

The syntax is as follows:

Selection.TypeText string
Selection.InsertAfter string
Selection.InsertBefore string

Here, string is a required String expression containing the text you want to insert in double quotation marks, as in this example:

Selection.TypeText "Please come to the meeting next Friday at 9:00 A.M."
Selection.InsertBefore "Dr. "
Selection.InsertAfter vbCr & Address

When you use the InsertAfter or the InsertBefore method, VBA extends the selection to include the text you inserted. (You can see selected text, cells, or other items in a document because Word changes the background from the default white to the document frame color.) When you use the TypeText method, the result is a collapsed selection—whether you are replacing a selection or a collapsed selection. (Recall that a collapsed selection means nothing is selected—merely the blinking insertion point.)

Inserting a Paragraph in a Selection

You can insert paragraphs:

  • To insert a paragraph at the current selection, use the InsertParagraph method.

  • To insert a paragraph before the current selection, use the InsertParagraphBefore method.

  • To insert a paragraph after the current selection, use the InsertParagraphAfter method.

You can also have VBA type a paragraph by using the Selection.TypeParagraph command.

Applying a Style

To apply a style to a paragraph, set the Style property of the Paragraph object:

Selection.Style = "Heading 3"

View the styles in the current document by pressing Ctrl+S, or click the Home tab on the Ribbon.

Similarly, you can apply a character style to the current selection or (as in the following example) to a specific range of words or characters. This example changes the fifth word in the second paragraph of the current document to boldface:

ActiveDocument.Paragraphs(2).Range.Words(5).Style = "Bold"

Note that a character style must always be applied to a range rather than directly to a paragraph.

Extending a Selection

To extend a selection, use the EndOf method for a Range or Selection object. The syntax for the EndOf method is as follows:

expression.EndOf(Unit, Extend)

Here, expression is a required expression that returns a Range or Selection object, such as an object in the Characters, Words, Sentences, or Paragraphs collection. Unit is an optional Variant specifying the unit of movement (see Table 20.6).

Table 20.6. Units of movement for the EndOf method

Unit

Meaning

wdCharacter

A character.

wdWord

A word. (This is the default setting if you omit the argument.)

wdSentence

A sentence.

wdLine

A line. (This unit can be used only with Selection objects, not with ranges.)

wdParagraph

A paragraph.

wdSection

A section of a document.

wdStory

The current story—for example, the document story or the header and footer story.

wdCell

A cell in a table.

wdColumn

A column in a table.

wdRow

A row in a table.

wdTable

A whole table.

Extend is an optional Variant specifying whether to move or extend the selection or range. wdMove moves the selection or range and is the default setting; wdExtend extends the selection or range.

For example, the following statement extends the current selection to the end of the paragraph:

Selection.EndOf Unit:=wdParagraph, Extend:=wdExtend

The following statement moves the selection to the end of the paragraph:

Selection.EndOf Unit:=wdParagraph, Extend:=wdMove

The following statement selects from the current selection to the end of the current Word story:

Selection.EndOf Unit:=wdStory, Extend:=wdExtend

To select the whole active document, use ActiveDocument.Content.Select. This command has the same effect as pressing Ctrl+A when working interactively.

Collapsing a Selection

When you've finished working with a selection larger than an insertion point, you often want to deselect it. In other words, you may want to have the selection in a collapsed state (just the blinking cursor) when the procedure ends. (If you don't do this and the user just starts typing, whatever is selected will be replaced by the user's typing.)

The easiest way to do so is to use the Collapse method of the Selection object to collapse the selection to its start or its end:

Selection.Collapse Direction:=wdCollapseStart
Selection.Collapse Direction:=wdCollapseEnd

Alternatively, you can reduce the selection to just one point by setting the selection's end selection equal to its start (collapsing the selection to its start) or by setting the selection's start equal to its end (collapsing the selection to its end):

Selection.End = Selection.Start
Selection.Start = Selection.End

Creating and Using Ranges

In Word, a range is a contiguous area of a document with a defined starting point and ending point. For example, if you define a range that consists of the first two paragraphs in a specified document, the range's starting point is at the beginning of the first paragraph, and its ending point is at the end of the second paragraph (after the paragraph mark).

A range is more flexible than a selection, and, most importantly, a range is named in your code, so you can refer to it by name at any time. There can be multiple ranges, but there can only be one selection at a time, and it has no name.

The typical use of ranges in Word VBA is similar to how you use bookmarks when working interactively with Word: to mark a location in a document that you want to be able to access quickly or manipulate easily. Like a bookmark, a range can contain any amount of text in a document, from a single character to all the contents of the document. A range can even have the same starting point and ending point, which gives it no contents and makes it, in effect, an invisible mark in the document that you can use to insert text. Once you've created a range, you can refer to it, access its contents or insert new contents in it, or format it, all by using the methods and properties of the range object.

Defining a Named Range

To create a Range object, you use a Set statement and either the Range method on a Document object or the Range property for an object—for example, the Selection object, the Paragraphs collection, or a Paragraph object. The syntax for using the Range method is as follows:

Set RangeName = Document.Range(Start, End)

Here, RangeName is the name you are assigning to the range, and Start and End are optional arguments specifying the starting and ending points of the range.

The syntax for using the Range property on an object is as follows:

Set RangeName = object.Range

For example, the following statement uses the Range property of the Paragraphs collection to define a range named FirstPara that consists of the first paragraph of the active document. This statement doesn't use Start and End arguments because the starting point and ending point of the paragraph are clearly understood:

Set FirstPara = ActiveDocument.Paragraphs(1).Range

The following statements change to uppercase the first three words at the start of a document:

Dim InitialCaps As Range
Set InitialCaps = ActiveDocument.Range _
(Start:=ActiveDocument.Words(1).Start, _
    End:=ActiveDocument.Words(3).End)
InitialCaps.Case = wdUpperCase

The first statement defines a Range object named InitialCaps. The second statement assigns InitialCaps to a range in the active document, from the beginning of the first word to the end of the third word. The third statement changes the case of the InitialCaps Range object to uppercase.

Because InitialCaps is now defined as a Range object for the duration of the procedure that declares it, you can return to InitialCaps and manipulate it later in the procedure if you want to.

Redefining a Range

To redefine a range to make it refer to another part of a document, use the SetRange method. The syntax is as follows:

expression.SetRange(Start, End)

Here, expression is a required expression that returns a Range or Selection object, and Start and End are optional arguments specifying the starting and ending points of the range.

For example, the following statement redefines the range named InitialCaps so it now refers to the first two characters of the document:

InitialCaps.SetRange Start:=0, End:=2

You can also redefine a range by reusing the Set method, creating the range again from scratch.

Using the Duplicate Property to Store or Copy Formatting

You can use the Duplicate property to store or copy a range so that you can apply it to another range. For example, the following statements declare two ranges, Range1 and Range2; store the duplicate of the current selection's range in Range1; assign to Range2 the Range of the first bookmark in the active document; and then apply to Range2 the contents of Range1:

Dim Range1 As Range, Range2 As Range
Set Range1 = Selection.Range.Duplicate
Set Range2 = ActiveDocument.Bookmarks(1).Range

Manipulating Options

In your procedures, you'll often need to check the status of options in the Word application or in a particular document. In VBA, many of the options are controlled by the Options object, which has dozens of properties but no methods.

The following sections show four brief examples of setting options, three examples using the Options object and one using a property of the Document object. To see the full list of properties available for the Options object, look in the Help system.

Making Sure Hyperlinks Require Ctrl+Clicking

Hyperlinks in Word documents have proved a mixed blessing—especially since Microsoft's changes to the way Word handles hyperlinks have left users unsure whether to just click or to Ctrl+click the hyperlink to follow it. You can set the CtrlClickHyperlinkToOpen property of the Options object to True to ensure that hyperlinks require Ctrl+clicking:

Options.CtrlClickHyperlinkToOpen = True

Setting this option to False means you can trigger links by merely clicking them—no Ctrl key required.

Turning Off Overtype

To make sure your procedures behave as expected, you may need to check that Word is using Insert mode rather than Overtype mode. (In Insert mode, Word inserts the characters you type at the insertion point, moving any text to the right of the characters over, to make room. In Overtype mode, each character you type replaces the character to the right of the insertion point.)

Overtype mode is controlled by the Overtype property of the Options object. When OverType is True, Overtype mode is on; when Overtype is False, Insert mode is on. The following statements store the user's current Overtype setting in a Boolean variable named blnOvertypeOn, set Overtype to False, perform its actions, and then restore the user's Overtype setting:

Dim blnOvertypeOn As Boolean
blnOvertypeOn = Options.Overtype
Options.Overtype = False   'write more code here to perform actions
Options.Overtype = blnOvertypeOn

Setting a Default File Path

When configuring Word on a computer, you may need to make sure that its default file paths are set to the correct folders. You can do so by working with the DefaultFilePath property of the Options object. The syntax is as follows:

expression.DefaultFilePath(Path)

Here, expression is a required expression that returns an Options object. Often, it's easiest to use the Options object itself. Path is one of the self-explanatory enumerated constants shown in the following list:

wdAutoRecoverPath

wdStyleGalleryPath

wdBorderArtPath

wdTempFilePath

wdCurrentFolderPath

wdTextConvertersPath

wdDocumentsPath

wdToolsPath

wdGraphicsFiltersPath

wdTutorialPath

wdPicturesPath

wdUserOptionsPath

wdProgramPath

wdUserTemplatesPath

wdProofingToolsPath

wdWorkgroupTemplatesPath

wdStartupPath

 

For example, the following statements set the user templates path and the workgroup templates path:

Options.DefaultFilePath(wdUserTemplatesPath) = _
"c:users
ichardappdata
oamingmicrosoft	emplates"
Options.DefaultFilePath(wdWorkgroupTemplatesPath) = _
"\serverusers	emplates"

Turning Off Track Changes

Before running a procedure that adds, deletes, or formats text, you may need to turn off the Track Changes feature so that the changes the procedure makes are not marked in the text. If the user had Track Changes on, you should turn it back on at the end of the procedure so that changes the user makes are tracked again. Remember that it's usually a good practice when changing options to first store the user's current setting in a variable, carry out your procedure's task, and then restore the user's original setting.

The following example saves the user's setting for the TrackRevisions option in the ActiveDocument object in a Boolean variable named blnTrackChangesOn, sets TrackRevisions to False, performs its actions, and then restores the user's TrackRevisions setting:

Dim blnTrackChangesOn As Boolean
blnTrackChangesOn = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
' write more code here to perform actions
ActiveDocument.TrackRevisions = blnTrackChangesOn

The Bottom Line

Understand the Word object model

Some people find viewing a schematic of the Word object model useful as a way of visualizing the interrelationships of the various objects and collections.

Master It

When you look at the Word Object Model Map, what is the highest object in the hierarchy—the object that contains all other objects?

Understand Word's creatable objects

Word contains a set of creatable objects that VBA programmers will frequently employ in their code.

Master It

What is a creatable object?

Work with the Documents collection and the Document object

The Documents collection represents all the currently open documents. Using VBA, you can manipulate this collection in a variety of ways.

Master It

Here is the syntax for creating a new document in the Documents collection:

Documents.Add Template, NewTemplate, DocumentType, Visible

If you merely want to add a new, empty document (based on the default Normal.dotm template) to the documents currently open in Word, the code is quite simple. What is the code that you would write in VBA to accomplish this?

Work with the Selection object

The Selection object represents the current selection in the active document in Word. A zone can be selected by the user by dragging the mouse or by using various key combinations (such as pressing Shift and an arrow key). A selection can include one or more objects—one or more characters, one or more words, one or more paragraphs, a graphic, a table, and so on—or a combination of these objects.

Master It

One kind of selection is described as a collapsed selection. What is that?

Create and use ranges

In Word, a range is a named contiguous area of a document with a defined starting and ending point. The typical use of ranges in Word VBA is similar to how you use bookmarks when working interactively with Word: to mark a location in a document that you want to be able to access quickly or manipulate easily.

Master It

Although a range is similar to a bookmark, what is the significant difference between them?

Manipulate options

Word contains many options that can be manipulated from within VBA.

Master It

In Word, one object controls many of the options. This object has dozens of properties but no methods. Name this object.

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

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