Find 方法

expandtri全部显示

该方法在标准模块类模块中查找指定的文本。

expression.Find(Target, StartLine, StartColumn, EndLine, EndColumn, WholeWord, MatchCase, PatternSearch)

expression     必需。返回“应用于”列表中的一个对象的表达式。

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

要确定找到的搜索文本在模块中的位置,请将 Find 方法的 StartLineStartColumnEndLineEndColumn 参数设为空。如果找到匹配的文本,这些参数将包含该搜索文本开始 (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