Share via


How to: Set Search Options in Word

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

There are two ways to set search options for selections in Microsoft Office Word documents:

Using Properties of a Find Object

The following code sets properties of a Find object to search for text within the current selection. Notice that the search criteria, such as searching forward, wrapping, and text to search for, are properties of the Find object.

Setting each of the properties of the Find object is not useful when you write C# code because you must specify the same properties as parameters in the Execute(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method. Therefore this example contains only Visual Basic code.

To set search options using a Find object

  • Set the properties of a Find object to search forward through a selection for the text find me.

    With Application.Selection.Find
        .ClearFormatting()
        .Forward = True
        .Wrap = Word.WdFindWrap.wdFindContinue
        .Text = "find me"
        .Execute()
    End With
    

Using Execute Method Arguments

The following code uses the Execute(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method of a Find object to search for text within the current selection. Notice that the search criteria, such as searching forward, wrapping, and text to search for, are passed as parameters of the Execute(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method.

To set search options using Execute method arguments

  • Pass search criteria as parameters of the Execute(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) method to search forward through a selection for the text find me.

    With Application.Selection.Find
        .ClearFormatting()
        .Execute(FindText:="find me", Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)
    End With
    
    object findText = "find me";
    object forward = true;
    object wrap = Word.WdFindWrap.wdFindContinue;
    
    Application.Selection.Find.ClearFormatting(); 
    
    Application.Selection.Find.Execute(
        ref findText, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref forward, ref wrap, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing);
    

See Also

Tasks

How to: Search for Text in Documents

How to: Search for and Replace Text in Documents

How to: Loop Through Found Items in Documents

How to: Restore Selections After Searches