替换代码模块

时间:2017-06-04 00:15:16

标签: access-vba vbe

我试图使用替换线功能来更新Access VBA模块中的代码。它不断出现编译错误。我检查了VBA扩展程序是否被选中并将其与我查找过的其他示例进行了比较。

这是我第一次使用这种功能,所以我还没有完全理解它们。

下面的代码

Sub ReplaceCodeModuleText(strModule As String, strFindWhat As String, strReplaceWith As String)
'FUNCTION:
'           Search the code module for specific text
'           Replace with new text


Dim VBProj As VBProject
Dim VBComp As VBComponent
Dim CodeMod As CodeModule


Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim strCodeLine As String
Dim vDummy As Variant

Dim Found As Boolean

    Set VBProj = Application.VBE.ActiveVBProject
    Set VBComp = VBProj.VBComponents(strModule)
    Set CodeMod = VBComp.CodeModule '    '.CodeModule

    With CodeMod
        SL = 1:        EL = .CountOfLines
        SC = 1:        EC = 255

        Found = .Find(Target:=strFindWhat, StartLine:=SL, StartColumn:=SC, _
            EndLine:=EL, EndColumn:=EC, _
            wholeword:=True, MatchCase:=False, patternsearch:=False)

        If Found Then
            strCodeLine = CodeMod.Lines(SL, 1)
            strCodeLine = Replace(strCodeLine, strFindWhat, strReplaceWith, Compare:=vbTextCompare) 'not case sensitive = vbTextCompare
            .ReplaceLine(SL, strCodeLine)

            Debug.Print "Successfully Replaced: " & strFindWhat & " in VBA Module: " & strModule & " with : " & strReplaceWith
        Else
            Debug.Print "Did not find: " & strFindWhat;

        End If
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

        .ReplaceLine(SL, strCodeLine)

必须是

        Call .ReplaceLine(SL, strCodeLine)

        .ReplaceLine SL, strCodeLine