Microsoft Word


Information on this page is from Microsoft MSDN http://msdn.microsoft.com.

Find and Replace (Word 2003)

Navigation through the various document objects is a usual feature of every word processor. For large documents (tens and hundred pages) navigation comforts became even more critical. Microsoft Word allows user to handy navigate through bookmarks, pages, sections, lines, paragraphs, pictures, tables, fields and headings. "Find", "Find Next" and "Find and Replace" are vital abilities of every word processor as well as "Go To" ability.  Microsoft Word allows user to search for formatting attributes as well as for ordinary text and regular expressions (also known as "search templates").

Search String

Search string could be "ordinary text" or regular expression . "Ordinary text" could contain special symbols such as line, paragraph or page break, optional hyphens etc. Regular expression contain wildcards - symbols  with special meaning.

"Find and Replace" Dialog

"Find and Replace" contains three tabs - "Find", "Replace" and "Go To".

Tab "Find"

Tab "Find" looks like this:

Popup "Format" menu allows user to add formatting attributes to the search criteria. The search criteria could contain Font, Paragraph, Tabs, Frame, Style attributes and Highlight.

 

Popup "Special" menu depends on the "Use wildcards" checkbox and looks like these:

Wildcards Off

Wildcards On

 

 

 

The Appendix contains "Special Symbols" dictionary.

Tab "Replace"

Tab "Replace" looks like this:

Popup "Format" menu allows user to add formatting attributes to the search criteria or to the replacement text - note "Find" or "Replace" label in the bottom part of the dialog depending on the.

If you need to add formatting attributes to the search criteria place caret to the "Find what" field first and then click "Format" button.
If you need to add formatting attributes to the replacement text place caret to the "Replace with" field first and then click "Format" button.

"Special" menu allows user to use special symbols in the search criteria or in the replacement text. "Special" menu depends on the caret position the same way as "Format" menu.

Tab "Go To"

Tab "Go To" looks like this:

 

Tab "Go To" allows user to navigate through pages, sections, lines, bookmarks, comments, footnotes and endnotes, fields, tables, graphics, equations, objects and headings.

Hot Keys

Hot keys for "Find", "Find Next", "Replace" and "Go To" operations are displayed in the "Edit" menu. Note that frequently used "Find Next" operation is not displayed in the "Edit" menu by default. You need to add it manually using "Customize" menu.

 

Find Object

Represents the criteria for a find operation. The properties and methods of the Find object correspond to the options in the Find and Replace dialog box.

Using the Find Object

Use the Find property to return a Find object. The following example finds and selects the next occurrence of the word "hi."

With Selection.Find
    .ClearFormatting
    .Text = "hi"
    .Execute Forward:=True
End With

The following example finds all occurrences of the word "hi" in the active document and replaces the word with "hello."

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="hi", ReplaceWith:="hello", Replace:=wdReplaceAll

Remarks

If you've gotten to the Find object from the Selection object, the selection is changed when text matching the find criteria is found. The following example selects the next occurrence of the word "blue."

Selection.Find.Execute FindText:="blue", Forward:=True

If you've gotten to the Find object from the Range object, the selection isn't changed when text matching the find criteria is found, but the Range object is redefined. The following example locates the first occurrence of the word "blue" in the active document. If "blue" is found in the document, myRange is redefined and bold formatting is applied to "blue."

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", Forward:=True
If myRange.Find.Found = True Then myRange.Bold = True

Find Object Properties

Find object properties are described in the following table.

Property

Type

Description

Font

Read/write Font

Returns or sets a Font object that represents the character formatting of the specified object. To set this property, specify an expression that returns a Font object.

Format

Read/write Boolean

True if formatting is included in the find operation.

Forward

Read/write Boolean

True if the find operation searches forward through the document. False if it searches backward through the document.

Found

Read-only Boolean

True if the search produces a match.

Frame

Frame Read-only

Returns a Frame object that represents the frame formatting for the specified style or find-and-replace operation. Read-only.

Highlight

Read/write Long

True if highlight formatting is included in the find criteria. Can return or be set to True, False, or wdUndefined. Note The wdUndefined value can be used with the Find object to ignore the state of highlight formatting in the selection or range that is searched.

LanguageID

Read/write

WdLanguageId

Returns or sets the language for the specified object.

MatchAllWordForms

Read/write Boolean

True if all forms of the text to find are found by the find operation (for instance, if the text to find is "sit," "sat" and "sitting" are found as well).

MatchByte

Read/write Boolean

True if Microsoft Word distinguishes between full-width and half-width letters or characters during a search.

MatchCase

Read/write Boolean

True if the find operation is case sensitive. The default is False.

MatchSoundsLike

Read/write Boolean

True if words that sound similar to the text to find are returned by the find operation.

MatchWholeWord

Read/write Boolean

True if the find operation locates only entire words and not text that's part of a larger word.

MatchWildcards

Read/write Boolean

True if the text to find contains wildcards. Corresponds to the Use wildcards check box in the Find and Replace dialog box (Edit menu).

NoProofing

Read/write Long

True if Microsoft Word finds or replaces text that the spelling and grammar checker ignores.

ParagraphFormat

ParagraphFormat

Read/write

Returns or sets a ParagraphFormat object that represents the paragraph settings for the specified range, selection, find or replacement operation, or style.

Replacement

Replacement

Returns a Replacement object that contains the criteria for a replace operation.

Style

Read/write Variant

Returns or sets the style for the specified object. To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style. For a list of valid constants, consult the Microsoft Visual Basic Object Browser.

Text

Read/write String

Returns or sets the text to find or replace in the specified range or selection.

Wrap

Read/write WdFindWrap

Returns or sets what happens if the search begins at a point other than the beginning of the document and the end of the document is reached (or vice versa if Forward is set to False) or if the search text isn't found in the specified selection or range.

Enumeration WdFindWrap

Controls what happens if the search begins at a point other than the beginning of the document and the end of the document is reached (or vice versa if Forward is set to False). This argument also controls what happens if there's a selection or range and the search text isn't found in the selection or range.

Constant

Value

Description

wdFindAsk

2

After searching the selection or range, Microsoft Word displays a message asking whether to search the remainder of the document.

wdFindContinue

1

The find operation continues if the beginning or end of the search range is reached.

wdFindStop

0

The find operation ends if the beginning or end of the search range is reached.

Find Object Methods

Find object methods are described in the following table.

Method

Description

ClearFormatting Removes text and paragraph formatting from a selection or from the formatting specified in a find or replace operation. Remarks To ensure that formatting isn't included as criteria in a find or replace operation, use this method before carrying out the operation.

Execute(
FindText,
MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap,
Format,
ReplaceWith,
Replace
)

Runs the specified find operation. Returns True if the find operation is successful. Boolean.

FindText   Optional Variant. The text to be searched for. Use an empty string ("") to search for formatting only. You can search for special characters by specifying appropriate character codes. For example, "^p" corresponds to a paragraph mark and "^t" corresponds to a tab character.

MatchCase   Optional Variant. True to specify that the find text be case sensitive. Corresponds to the Match case check box in the Find and Replace dialog box (Edit menu).

MatchWholeWord   Optional Variant. True to have the find operation locate only entire words, not text that's part of a larger word. Corresponds to the Find whole words only check box in the Find and Replace dialog box.

MatchWildcards   Optional Variant. True to have the find text be a special search operator. Corresponds to the Use wildcards check box in the Find and Replace dialog box.

MatchSoundsLike   Optional Variant. True to have the find operation locate words that sound similar to the find text. Corresponds to the Sounds like check box in the Find and Replace dialog box.

MatchAllWordForms   Optional Variant. True to have the find operation locate all forms of the find text (for example, "sit" locates "sitting" and "sat"). Corresponds to the Find all word forms check box in the Find and Replace dialog box.

Forward   Optional Variant. True to search forward (toward the end of the document).

Wrap   Optional Variant. Controls what happens if the search begins at a point other than the beginning of the document and the end of the document is reached (or vice versa if Forward is set to False). This argument also controls what happens if there's a selection or range and the search text isn't found in the selection or range. Can be one of the following WdFindWrap constants.

Format   Optional Variant. True to have the find operation locate formatting in addition to or instead of the find text.

ReplaceWith   Optional Variant. The replacement text. To delete the text specified by the Find argument, use an empty string (""). You specify special characters and advanced search criteria just as you do for the Find argument. To specify a graphic object or other nontext item as the replacement, move the item to the Clipboard and specify "^c" for ReplaceWith.

Replace   Optional Variant. Specifies how many replacements are to be made: one, all, or none. Can be any WdReplace constant.

MatchKashida   Optional Variant. True if find operations match text with matching kashidas in an Arabic language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that youve selected or installed.

MatchDiacritics   Optional Variant. True if find operations match text with matching diacritics in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that youve selected or installed.

MatchAlefHamza   Optional Variant. True if find operations match text with matching Alef Hamzas in an Arabic language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that youve selected or installed.

MatchControl   Optional Variant. True if find operations match text with matching bidirectional control characters in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that youve selected or installed.

Remarks

If MatchWildcards is True, you can specify wildcard characters and other advanced search criteria for the FindText argument. For example, "*(ing)" finds any word that ends in "ing."

To search for a symbol character, type a caret (^), a zero (0), and then the symbol's character code. For example, "^0151" corresponds to an em dash ( ).

Unless otherwise specified, replacement text inherits the formatting of the text it replaces in the document. For example, if you replace the string "abc" with "xyz," occurrences of "abc" with bold formatting are replaced with the string "xyz" with bold formatting.

Also, if MatchCase is False, occurrences of the search text that are uppercase will be replaced with an uppercase version of the replacement text regardless of the case of the search and replacement text. Using the previous example, occurrences of "ABC" are replaced with "XYZ."

Enumeration WdReplace

WdReplace enumeration sets the amount of replacements.

Constant

Value

wdReplaceAll

2

wdReplaceNone

0

wdReplaceOne

1

Replacement Object

Represents the replacement text attributes for a find and replace operation. The properties and methods of the Replacement object correspond to the options in the Find and Replace dialog box.

Replacement Objects Properties and Methods

The single method ClearFormatting does the same as corresponding method of the Find object.

Replacement object properties are described in the following table.

Property

Type

Description

Font

Read/write Font

Returns or sets a Font object that represents the character formatting of the specified object. To set this property, specify an expression that returns a Font object.

Frame

Frame Read-only

Returns a Frame object that represents the frame formatting for the specified style or find-and-replace operation.

Highlight

Read/write Long

True if highlight formatting is applied to the replacement text. Can return or be set to True, False, or wdUndefined.

LanguageID

Read/write WdLanguageId

Returns or sets the language for the specified object.

NoProofing

Read/write Long

True if Microsoft Word finds or replaces text that the spelling and grammar checker ignores.

ParagraphFormat

ParagraphFormat Read/write

Returns or sets a ParagraphFormat object that represents the paragraph settings for the specified range, selection, find or replacement operation, or style.

Style

Read/write Variant

Returns or sets the style for the specified object. To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style. For a list of valid constants, consult the Microsoft Visual Basic Object Browser.

Text

Read/write String

Returns or sets the text to find or replace in the specified range or selection.

Regular Expressions

Wildcards

Code

Object

?

Any single character. For example, s?t finds "sat" and "set".

*

Any string of characters. For example, s*d finds "sad" and "started".

The beginning of a word. For example, <(inter) finds "interesting" and "intercept", but not "splintered".

The end of a word. For example, (in)> finds "in" and "within", but not "interesting".

[ ]

One of the specified characters. For example, w[io]n finds "win" and "won".

[-]

Any single character in this range. For example, [r-t]ight finds "right" and "sight". Ranges must be in ascending order.

[!x-z]

Any single character except the characters in the range inside the brackets.
For example, t[!a-m]ck finds "tock" and "tuck", but not "tack" or "tick".

{n}

Exactly n occurrences of the previous character or expression.
For example, fe{2}d finds "feed" but not "fed".

{n,}

At least n occurrences of the previous character or expression.
For example, fe{1,}d finds "fed" and "feed".

{n,m}

From n to m occurrences of the previous character or expression.
For example, 10{1,3} finds "10", "100", and "1000".

@

One or more occurrences of the previous character or expression.
For example, lo@t finds "lot" and "loot".

Notes

  • You can use parentheses to group the wildcard characters and text and to indicate the order of evaluation. For example, type <(pre)*(ed)> to find "presorted" and "prevented".

  • You can use the \n wildcard to search for an expression and then replace it with the rearranged expression. For example, type (Newton) (Christie) in the Find what box and \2 \1 in the Replace with box. Word will find "Newton Christie" and replace it with "Christie Newton".

'Find and Replace' Codes

Code

Object

^p ^13

Paragraph mark.
Type ^p (doesnt work in the Find what box when wildcards are on) or type ^13.

^t ^9

Tab character. Type ^t or type ^9.

^nnn

ASCII character. Type ^nnn, where nnn is the character code.

^0nnn

ANSI character. Type ^0nnn, where 0 is zero and nnn is the character code.

^+

Em dash ( ). Type ^+

^=

En dash ( ). Type ^=

^^

Caret character

^l ^11

Manual line break. Type ^l or type ^11

^n ^14

Column break. Type ^n or type ^14.

^12

Page or section break. Type ^12 (when replacing, inserts a page break).

^m

Manual page break. Type ^m (also finds or replaces section breaks when wildcards are on).

^s

Nonbreaking space. Type ^s

^~

Nonbreaking hyphen. Type ^~

^-

Optional hyphen. Type ^-

'Find' Codes (Regular Expressions)

Code

Object

^g

Picture or graphic (inline only).

'Find' Codes (Simple Find)

Code

Object

^?

Any character. Type ^?

^#

Any digit. Type ^#

^$

Any letter . Type ^$

^Unnnn

Unicode character. Type ^Unnnn where nnnn is the character code.

^1

Picture or graphic (inline only). Type ^1

^f ^2

Footnote mark. Type ^f or type ^2.

^e

Endnote mark. Type ^e

^d

Field. Type ^d

^19

Opening field brace (when field codes are visible). Type ^19

^21

Closing field brace (when field codes are visible). Type ^21

^a ^5

Comment. Type ^a or type ^5

^b

Section break. Type ^b

^u8195

Em space (Unicode). Type ^u8195

^u8194

En space (Unicode). Type ^u8194

^w

White space. Type ^w (any combination of regular and nonbreaking spaces, and tab characters).

'Replacement' Codes

^c

Windows Clipboard contents. Type ^c

^&

Contents of the Find what box. Type ^&

Notes
  • If you include the optional hyphen code, Word finds only text with optional hyphens in the position you specified. If you omit the optional hyphen code, Word finds all matching text, including text with optional hyphens.

  • If you're searching for fields, you must display field codes. To switch between displaying field codes and field results, click the field, and press SHIFT+F9. To show or hide field codes for all fields in the document, press ALT+F9.

  • If you're searching for graphics, Word finds only inline graphics; Word does not find floating graphics. By default, Word inserts imported graphics as inline graphics, but you can change a floating graphic to an inline graphic.

  • To find a character using the Unicode value, select the Match Case check box. If you clear the Match Case check box, Word searches all instances of uppercase and lowercase characters specified by the value.


Etudes for Microsoft Word Programmers. Appendix. Find and Replace.


Unless otherwise noted, all materials on this site are
2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site.
www.transcriber.ru | E-mail the Author