How to: Restore Selections After Searches
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
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
If you find and replace text in a document, you might want to restore the user's original selection after the search is completed.
The code in the sample procedure makes use of two Range objects. One stores the current Selection, and one sets the entire document to use as a search range.
To restore the user's original selection after a search
Create the Range objects for the document and the current selection.
Dim start As Word.Range = Application.Selection.Range Dim searchArea As Word.Range = Application.ActiveDocument.Range
Word.Range start = Application.Selection.Range; Word.Range searchArea = Application.ActiveDocument.Range(ref missing, ref missing);
Perform the search and replace operation.
searchArea.Find.ClearFormatting() searchArea.Find.Text = "find me" searchArea.Find.Replacement.ClearFormatting() searchArea.Find.Replacement.Text = "Found" searchArea.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
searchArea.Find.ClearFormatting(); searchArea.Find.Text = "find me"; searchArea.Find.Replacement.ClearFormatting(); searchArea.Find.Replacement.Text = "Found"; object replaceAll = Word.WdReplace.wdReplaceAll; searchArea.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
Select the start range to restore the user's original selection.
start.Select()
start.Select();
The following example shows the complete method.
Example
Friend Sub ReplaceRestoreSelection()
Dim start As Word.Range = Application.Selection.Range
Dim searchArea As Word.Range = Application.ActiveDocument.Range
searchArea.Find.ClearFormatting()
searchArea.Find.Text = "find me"
searchArea.Find.Replacement.ClearFormatting()
searchArea.Find.Replacement.Text = "Found"
searchArea.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
start.Select()
End Sub
internal void ReplaceRestoreSelection()
{
Word.Range start = Application.Selection.Range;
Word.Range searchArea = Application.ActiveDocument.Range(ref missing, ref missing);
searchArea.Find.ClearFormatting();
searchArea.Find.Text = "find me";
searchArea.Find.Replacement.ClearFormatting();
searchArea.Find.Replacement.Text = "Found";
object replaceAll = Word.WdReplace.wdReplaceAll;
searchArea.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
start.Select();
}
See Also
Tasks
How to: Search for and Replace Text in Documents
How to: Search for Text in Documents
How to: Set Search Options in Word
How to: Loop Through Found Items in Documents
Concepts
The Variable missing and Optional Parameters in Office Solutions