替换代码模块中的文本 [英] replace text in code module

查看:57
本文介绍了替换代码模块中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用替换行功能来​​更新 Access VBA 模块中的代码.它不断出现编译错误.我已经检查过是否选择了 VBA 扩展并将其与我查找过的其他示例进行了比较.

Im trying to use the replaceline function to update code in Access VBA module. it keeps coming up with a compile error. Ive checked that the VBA Extension are selected and compared it to other examples that I have looked up.

这是我第一次使用这种类型的功能,所以我还没有完全了解它们.

this is the first time that Ive used this type of function, so I haven't fully got my head around them.

代码如下

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

推荐答案

        .ReplaceLine(SL, strCodeLine)

必须是其中之一

        Call .ReplaceLine(SL, strCodeLine)

        .ReplaceLine SL, strCodeLine

这篇关于替换代码模块中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆