共用方式為


HOW TO:在文件中搜尋文字

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2003

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

Find 物件是 SelectionRange 物件都有的成員,您可以使用這個成員在 Microsoft Office Word 文件中搜尋文字。取代命令是尋找命令的擴充。如需在文件中取代文字的詳細資訊,請參閱 HOW TO:搜尋和取代文件中的文字

使用 Selection 物件

使用 Selection 物件尋找文字時,所指定的任何搜尋準則都只會套用至目前選取的文字。如果 Selection 是插入點,則會搜尋文件。找到符合搜尋準則的項目時,會自動選取該項目。

請特別注意,Find 準則是累計式的,這意味著準則會加入先前的搜尋準則。進行搜尋前,請使用 ClearFormatting 方法清除之前搜尋的格式設定。

若要使用 Selection 物件尋找文字

  1. 將搜尋字串指派至變數。

    Dim findText As String = "find me"
    
    object findText = "find me";
    
  2. 清除之前搜尋的格式設定。

    Application.Selection.Find.ClearFormatting()
    
    Application.Selection.Find.ClearFormatting();
    
  3. 執行搜尋並顯示含有結果的訊息方塊。

    If Application.Selection.Find.Execute(findText) = True Then
        MessageBox.Show("Text found.")
    Else
        MessageBox.Show("The text could not be located.")
    End If
    
    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.");
    } 
    

下列程式碼範例示範完整的方法:

Private Sub SelectionFind()
    Dim findText As String = "find me"

    Application.Selection.Find.ClearFormatting()

    If Application.Selection.Find.Execute(findText) = True Then
        MessageBox.Show("Text found.")
    Else
        MessageBox.Show("The text could not be located.")
    End If
End Sub
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.");
    } 
}

使用 Range 物件

使用 Range 物件可讓您搜尋文字,而不必在使用者介面中顯示任何資訊。如果找到符合搜尋準則的文字,則 Find 物件會傳回 True,否則傳回 False。如果找到文字,該方法還會重新定義 Range 物件以符合搜尋準則。

若要使用 Range 物件尋找文字

  1. 定義一個由文件第二段組成的 Range 物件。

    下列程式碼範例可以用於文件層級自訂中。

    Dim rng As Word.Range = Me.Paragraphs(2).Range
    
    Word.Range rng = this.Paragraphs[2].Range; 
    

    下列程式碼範例可以用於應用程式層級的增益集中。本範例使用主動式文件。

    Dim rng As Word.Range = Me.Application.ActiveDocument.Paragraphs(2).Range
    
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Paragraphs[2].Range;
    
  2. 使用 Range 物件的 Find 屬性,先清除任何現有的格式選項,然後搜尋字串 find me。

    rng.Find.ClearFormatting()
    
    If rng.Find.Execute(findText) Then
    
    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. 在訊息方塊中顯示搜尋結果,並選取 Range 讓它成為可見的樣子。

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

    如果搜尋失敗,第二段會被選取;如果成功,就會顯示搜尋準則。

下列範例顯示文件層級自訂的完整程式碼。若要使用這個範例,請從專案的 ThisDocument 類別 (Class) 中執行程式碼。

Private Sub RangeFind()
    Dim findText As String = "find me"

    Dim rng As Word.Range = Me.Paragraphs(2).Range

    rng.Find.ClearFormatting()

    If rng.Find.Execute(findText) Then
        MessageBox.Show("Text found.")
    Else
        MessageBox.Show("Text not found.")
    End If

    rng.Select()
End Sub
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(); 
}

下列範例顯示應用程式層級增益集的完整程式碼。若要使用這個範例,請從專案的 ThisAddIn 類別中執行程式碼。

Private Sub RangeFind()
    Dim findText As Object = "find me"

    Dim rng As Word.Range = Me.Application.ActiveDocument.Paragraphs(2).Range

    rng.Find.ClearFormatting()

    If rng.Find.Execute(findText) Then
        MessageBox.Show("Text found.")
    Else
        MessageBox.Show("Text not found.")
    End If

    rng.Select()
End Sub
private void RangeFind()
{
    object findText = "find me";

    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.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();
}

請參閱

工作

HOW TO:搜尋和取代文件中的文字

HOW TO:在 Word 中設定搜尋選項

HOW TO:在文件中找到的項目之間執行迴圈

HOW TO:在文件中定義及選取範圍

HOW TO:在搜尋後還原選取

概念

了解 Office 方案中的選擇性參數