ITextRange::FindText 方法 (tom.h)

最多搜尋bstr所指定文字的Count字元。 起始位置和方向也會由 Count指定,而比對準則則由 Flags指定。

語法

HRESULT FindText(
  BSTR bstr,
  long Count,
  long Flags,
  long *pLength
);

參數

bstr

類型: BSTR

要尋找的字串。

Count

類型: long

要搜尋的字元數上限。 它可以是下列其中一項。

tomForward 搜尋到本文結尾。 這是預設值。
n (大於 0) cpFirst開始,向前搜尋n個字元。如果範圍本身符合bstr,則會嘗試從cpFirst + 1 進行另一個搜尋。
n (小於 0) cpLim開始向後搜尋n個字元。如果範圍本身符合bstr,則會嘗試從cpLim – 1 進行另一個搜尋。
0 (變質範圍) 搜尋會在範圍之後開始。
0 (非產生範圍) 搜尋僅限於範圍。
 

在所有情況下,如果找到字串,範圍限制會變更為相符字串的限制,並將 pLength 設定為等於字串的長度。 如果找不到字串,範圍會保持不變,且 pLength 設定為零。

Flags

類型: long

控管比較的旗標。 它可以是 0 (預設) 或下列值的任何組合。

tomMatchWord 2 比對整字。
tomMatchCase 4 符合大小寫。
tomMatchPattern 8 比對正則運算式。

pLength

類型: long*

相符字串的長度。

傳回值

類型: HRESULT

方法會傳回 HRESULT 值。 如果方法成功,它會 傳回S_OK。 如果方法失敗,它會傳回S_FALSE。 如需 COM 錯誤碼的詳細資訊,請參閱 COM 中的錯誤處理

備註

ITextRange::FindText方法也可以使用插入號 (^) 後面加上特殊字母來比對特殊字元。 如需特殊字元的清單,請參閱 Microsoft Word [尋找及取代] 對話方塊中可用的特殊清單。 例如, ^p 符合下一個段落標記。 請注意, ^c 可用來代表要取代之字串中的剪貼簿內容。 因此,在 ^c 尋找字串中使用 可讓您搜尋 RTF。 如需詳細資訊,請參閱Word說明檔。

相較于 ITextRange::FindText 方法, ITextRange::FindTextStart 方法會從範圍的 Start cp向前或向後搜尋,而 ITextRange::FindTextEnd 方法會從範圍的 End cp向前或向後搜尋。 如需詳細資訊,請參閱這些方法的描述。

以下是顯示 ITextRange::FindText 方法的數個程式碼片段。

範例 #1。 下列 Microsoft Visual Basic for Applications (VBA) 程式會在範圍 r 所識別的本文中列印所有 /* ... */ 批註。

Sub PrintComments (r As ITextRange)
    r.SetRange 0, 0                                      'r = insertion pt at start of story
    Do While r.FindText("/*") And r.FindTextEnd("*/")    'Select comment
        r.MoveStart tomCharacter, 2                      'But do not include the opening or 
                                                         'closing comment brackets
        r.MoveEnd tomCharacter, -2                       
        Print r                                          'Show the folks
    Loop
End Sub

這些批註可以插入另一個編輯實例並儲存至檔案,或者可以插入資料表或試算表中的個別儲存格,而不是列印這些批註。

若要列印包含 「laser」 一字出現次數的所有行,請以下列程式碼取代迴圈:

    While r.FindText("laser")            // Select next occurrence of "laser"
        r.Expand tomLine                // Select enclosing line    
        Print r                    // Print the line
    Wend

範例 #2。 下列程式會列印電話清單,指定包含通訊清單的劇本。 通訊清單專案會以兩個或多個段落標記分隔,而且每個專案都有下列格式。

Person/Business Name
Address (one or more lines)
(area code) telephone number 

請注意,使用 FindText字串引數中的 字元 ^p 來尋找一對連續段落標記。

Sub PrintTelephoneList (r As ITextRange)
    r.SetRange 0, 0                 // r = insertion point at start of story
    r.MoveWhile C1_WHITE            // Bypass any initial white space
    Do
        r.EndOf tomParagraph, 1     // Select next para (line): has name
        Print r                    // Print it
        Do
            r.MoveWhile C1_SPACE        // Bypass possible space chars
            If r.Char = Asc("(") Then Exit Do    // Look for start of telephone #
        Loop While r.Move(tomParagraph)    // Go to next paragraph
        r.EndOf tomParagraph, 1        // Select line with telephone number
        Print r                    // Print it
    Loop While r.FindText("^p^p")        // Find two consecutive para marks
End Sub

範例 #3。 下列副程式會以 str2 取代範圍中所有出現的字串 str1:

Sub Replace ( tr As ITextRange, str1 As String, str2 As String )
    Dim r As ITextRange
    r = tr.Duplicate                // Copy tr parameters to r
    r.End = r.Start                    // Convert to insertion point at Start
    While r.FindText(str1, tr.End - r.End)        // Match next occurrence of str
        r = str2                // Replace it with rep
    Wend                        // Iterate till no more matches
End Sub

範例 #4。 下列程式碼會在第一次出現右括弧 「 (」 之前插入空白,該括弧緊接在 HRESULT 的後面。

    If r.FindText("HRESULT") And r.FindText("(") Then r = " ("

若要針對所有這類專案執行此動作,請將 If 變更為上述程式程式碼中的 While/Wend 迴圈。 這是無法使用[尋找和取代] 對話方塊執行的FIND/REPLACE宏範例。

規格需求

   
最低支援的用戶端 Windows Vista [僅限傳統型應用程式]
最低支援的伺服器 Windows Server 2003 [僅限桌面應用程式]
目標平台 Windows
標頭 tom.h
Dll Msftedit.dll

另請參閱

概念

FindTextEnd

FindTextStart

ITextRange

參考

文字物件模型