Export (0) Print
Expand All
Expand Minimize

How to: Search for Text in Documents

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

The Find object is a member of both the Selection and the Range objects, and you can use either one to search for text in Microsoft Office Word 2003 documents. The replace command is an extension of the find command. For information on replacing text in documents, see How to: Search for and Replace Text in Documents.

Using a Selection Object

When you use a Selection object to find text, any search criteria you specify are applied only against currently selected text. If the Selection is an insertion point, then the document is searched. When the item is found that matches the search criteria, it is automatically highlighted.

It is important to note that the Find criteria are cumulative, which means that criteria are added to previous search criteria. Clear formatting from previous searches by using the ClearFormatting method prior to the search.

To find text using a Selection object

  1. Assign a search string to a variable.

    object findText = "find me";
    
    
  2. Clear formatting from previous searches.

    Application.Selection.Find.ClearFormatting();
    
    
  3. Execute the search and display a message box with the results.

    if (Application.Selection.Find.Execute(ref findText,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing)) 
    { 
        MessageBox.Show("Text found.");
    } 
    else
    { 
        MessageBox.Show("The text could not be located.");
    } 
    
    

The following example shows the complete method.

private void SelectionFind() 
{ 
    object findText = "find me";

    Application.Selection.Find.ClearFormatting();

    if (Application.Selection.Find.Execute(ref findText,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing)) 
    { 
        MessageBox.Show("Text found.");
    } 
    else
    { 
        MessageBox.Show("The text could not be located.");
    } 
}

Using a Range Object

Finding text using a Range object allows you to search for text without displaying anything in the user interface. The Find object returns True if text is found that matches the search criteria, and False if it does not. It also redefines the Range object to match the search criteria if the text is found.

To find text using a Range object

  1. Define a Range object consisting of the second paragraph in the document.

    Word.Range rng = this.Paragraphs[2].Range; 
    
    
  2. Use the Find property of the Range object, first clearing any existing formatting options, and then search for the string find me.

    rng.Find.ClearFormatting();
    
    if (rng.Find.Execute(ref findText,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing)) 
    { 
    
    
  3. Display the results of the search in a message box, and select the Range to make it visible.

        MessageBox.Show("Text found.");
    } 
    else 
    { 
        MessageBox.Show("Text not found.");
    } 
    
    rng.Select(); 
    
    

    If the search fails, the second paragraph is selected; if it succeeds, the search criteria are displayed.

The complete method looks like this:

private void RangeFind() 
{ 
    object findText = "find me";

    Word.Range rng = this.Paragraphs[2].Range; 

    rng.Find.ClearFormatting();

    if (rng.Find.Execute(ref findText,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing)) 
    { 
        MessageBox.Show("Text found.");
    } 
    else 
    { 
        MessageBox.Show("Text not found.");
    } 

    rng.Select(); 
}

See Also

Community Additions

ADD
Show:
© 2014 Microsoft