共用方式為


Module.Find 方法 (Access)

在標準模組或類別模組中尋找指定文字。

語法

運算式尋找 (目標StartLineStartColumnEndLineEndColumnWholeWordMatchCasePatternSearch)

表達 代表 Module 物件的變數。

參數

名稱 必要/選用 資料類型 描述
Target 必要 String 要尋找的文字。
StartLine 必要 Long 開始搜尋的行號。 如果找到相符專案, StartLine 引數的值會設定為找到相符文字開頭字元的行。
StartColumn 必要 Long 開始搜尋的欄位。 每一行中的每一字元都在個別的欄中,模組左邊的開始欄位數為零。 如果找到相符專案, StartColumn 自 變數的值會設定為找到相符文字開頭字元的資料行。
EndLine 必要 Long 停止搜尋的行號。 如果找到相符專案, EndLine 引數的值會設定為找到相符文字之結束符的行。
EndColumn 必要 Long 停止搜尋的欄位。 如果找到相符專案, EndColumn 自 變數的值會設定為找到相符文字開頭字元的資料行。
WholeWord 選用 Boolean True 會產生搜尋全字拼寫須相符。 預設值為 False
MatchCase 選用 Boolean 則為 true 會產生搜尋文字的大小寫比對 Target引數使用。 預設值為 False
PatternSearch 選用 Boolean True 會產生順序 Target引數的可能包含星號 (*) 或問號 (?) 之類的萬用字元搜尋。 預設值為 False

傳回值

布林值

註解

Find 方法搜尋的 Module 物件中指定的文字字串。 如果有找到字串, Find 方法會傳回 True

若要判斷在模組中找到搜尋文字的位置,請將空白變數傳遞至StartLineStartColumnEndLineEndColumn引數的Find方法。 如果找到相符的文字,這些引數將包含搜尋文字開始 (StartLine, StartColumn) 和結束 (EndLine, EndColumn) 的行號與欄位位置。

例如,如果在第 5 行找到搜尋文字,從第 10 欄開始,並結束于欄 20,則這些引數的值會是 StartLine = 5, StartColumn = 10, EndLine = 5, EndColumn = 20。

範例

下列函數會在模組中尋找指定的字串,並將包含該字串的行以新指定的行取代。

Function FindAndReplace(strModuleName As String, _ 
 strSearchText As String, _ 
 strNewText As String) As Boolean 
 Dim mdl As Module 
 Dim lngSLine As Long, lngSCol As Long 
 Dim lngELine As Long, lngECol As Long 
 Dim strLine As String, strNewLine As String 
 Dim intChr As Integer, intBefore As Integer, _ 
 intAfter As Integer 
 Dim strLeft As String, strRight As String 
 
 ' Open module. 
 DoCmd.OpenModule strModuleName 
 ' Return reference to Module object. 
 Set mdl = Modules(strModuleName) 
 
 ' Search for string. 
 If mdl.Find(strSearchText, lngSLine, lngSCol, lngELine, _ 
 lngECol) Then 
 ' Store text of line containing string. 
 strLine = mdl.Lines(lngSLine, Abs(lngELine - lngSLine) + 1) 
 ' Determine length of line. 
 intChr = Len(strLine) 
 ' Determine number of characters preceding search text. 
 intBefore = lngSCol - 1 
 ' Determine number of characters following search text. 
 intAfter = intChr - CInt(lngECol - 1) 
 ' Store characters to left of search text. 
 strLeft = Left$(strLine, intBefore) 
 ' Store characters to right of search text. 
 strRight = Right$(strLine, intAfter) 
 ' Construct string with replacement text. 
 strNewLine = strLeft & strNewText & strRight 
 ' Replace original line. 
 mdl.ReplaceLine lngSLine, strNewLine 
 FindAndReplace = True 
 Else 
 MsgBox "Text not found." 
 FindAndReplace = False 
 End If 
 
Exit_FindAndReplace: 
 Exit Function 
 
Error_FindAndReplace: 
 
MsgBox Err & ": " & Err.Description 
 FindAndReplace = False 
 Resume Exit_FindAndReplace 
End Function

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應