How to: Programmatically Set Search Options in Word

How to: Programmatically Set Search Options in Word

 

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

Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Word. For more information, see Features Available by Office Application and Project Type.

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 M:Microsoft.Office.Interop.Word.Find.Execute(System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@) method. Therefore this example contains only Visual Basic code.

To set search options using a Find object

  1. 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
    

The following code uses the M:Microsoft.Office.Interop.Word.Find.Execute(System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.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 M:Microsoft.Office.Interop.Word.Find.Execute(System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@) method.

To set search options using Execute method arguments

  1. Pass search criteria as parameters of the M:Microsoft.Office.Interop.Word.Find.Execute(System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@,System.Object@) method to search forward through a selection for the text find me.

                Application.Selection.Find.ClearFormatting(); 
                Application.Selection.Find.Execute("find me", Forward:true, Wrap:Word.WdFindWrap.wdFindContinue);
    

How to: Programmatically Search for and Replace Text in Documents
How to: Programmatically Loop Through Found Items in Documents
How to: Programmatically Restore Selections After Searches

Show:
© 2016 Microsoft