Module.Find 方法 (Access)

在标准模块或类模块中查找指定的文本。

语法

表达式查找 (TargetStartLineStartColumnEndLineEndColumnWholeWordMatchCasePatternSearch)

表达 一个代表 Module 对象的变量。

参数

名称 必需/可选 数据类型 说明
Target 必需 String 要查找的文本。
StartLine 必需 Long 搜索的起始行。 如果找到匹配项,则 StartLine 参数的值设置为匹配文本的起始字符所在的行。
StartColumn 必需 Long 搜索的起始列。 行中的每一字符都位于模块左侧的不同列(列号从零开始)。 如果找到匹配项,则 StartColumn 参数的值将设置为在其中找到匹配文本的起始字符的列。
EndLine 必需 Long 搜索的结束行。 如果找到匹配项,则 EndLine 参数的值设置为找到匹配文本的结束字符所在的行。
EndColumn 必需 Long 搜索的结束列。 如果找到匹配项, EndColumn 参数的值将设置为在其中找到匹配文本的起始字符的列。
WholeWord 可选 Boolean True 将导致全字匹配搜索。 默认值为 False
MatchCase 可选 Boolean 会导致搜索词的大小写匹配的 Target参数。 默认值为 False
PatternSearch 可选 Boolean 则返回 true ,则的搜索 Target参数可以包含通配符字符,如星号 (*) 或问号 (?)。 默认值为 False

返回值

布尔值

备注

Find 方法搜索在 模块 对象中指定的文本字符串。 如果找到该字符串,则 Find 方法返回 ,则返回 True

若要确定在模块中查找搜索文本的位置,请将空变量传递给 StartLineStartColumnEndLineEndColumn 参数的 Find 方法。 如果找到匹配的文本,这些参数将包含该搜索文本开始(StartLineStartColumn)和结束(EndLineEndColumn)的行编号和列的位置。

例如,如果在第 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。