CodeModule.ReplaceLine导致崩溃

时间:2018-09-08 05:58:25

标签: ms-access access-vba

我在VBA中有一个函数,可以在代码运行时替换代码。我使用“ CodeModule.ReplaceLine”来做到这一点。这主要是为了更新网址中的Cookie。重写文本行时,正在更新的功能未运行。 我最初在Access 2013中使用过,但现在使用2016。

我怀疑这是因为代码在编译后就被更改了。但我不确定为什么现在而不是以前有问题。代码如下。

Sub ReplaceCodeModuleText(sModule As String, sProcName As String, sFindWhat As String, sReplaceWith 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 Proc As Procedure

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 sCodeLine As String
Dim vDummy As Variant

Dim Found As Boolean

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

    With CodeMod
        SL = .ProcStartLine(sProcName, vbext_pk_Proc)
        EL = SL + .ProcCountLines(sProcName, vbext_pk_Proc) - 1
        SC = 1:        EC = 255

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

        If Found Then
            sCodeLine = CodeMod.Lines(SL, 1)
            sCodeLine = Replace(sCodeLine, sFindWhat, sReplaceWith, Compare:=vbTextCompare) 'not case sensitive = vbTextCompare
            Call .ReplaceLine(SL, sCodeLine)

            Debug.Print "Successfully Replaced: " & sFindWhat & " in VBA Module: " & sModule & " with : " & sReplaceWith
        Else
            Debug.Print "Did not find: " & sFindWhat;

        End If
    End With
End Sub

0 个答案:

没有答案