使用 VBA 替换 MS Access 中的模块文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/865530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:25:37  来源:igfitidea点击:

Replace Module Text in MS Access using VBA

ms-accessvba

提问by Greg Finzer

How do I do a search and replace of text within a module in Access from another module in access? I could not find this on Google.

如何在 Access 中的另一个模块中搜索和替换 Access 中的一个模块中的文本?我在谷歌上找不到这个。

FYI, I figured out how to delete a module programatically:

仅供参考,我想出了如何以编程方式删除模块:

Call DoCmd.DeleteObject(acModule, modBase64)

调用 DoCmd.DeleteObject(acModule, modBase64)

采纳答案by Greg Finzer

After a lot of searching I found this code:

经过大量搜索,我找到了这段代码:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function to Search for a String in a Code Module. It will return True if it is found and
'False if it is not. It has an optional parameter (NewString) that will allow you to
'replace the found text with the NewString. If NewString is not included in the call
'to the function, the function will only find the string not replace it.
'
'Created by Joe Kendall 02/07/2003
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function SearchOrReplace(ByVal ModuleName As String, ByVal StringToFind As String, _
        Optional ByVal NewString, Optional ByVal FindWholeWord = False, _
        Optional ByVal MatchCase = False, Optional ByVal PatternSearch = False) As Boolean

    Dim mdl As Module
    Dim lSLine As Long
    Dim lELine As Long
    Dim lSCol As Long
    Dim lECol As Long
    Dim sLine As String
    Dim lLineLen As Long
    Dim lBefore As Long
    Dim lAfter As Long
    Dim sLeft As String
    Dim sRight As String
    Dim sNewLine As String

    Set mdl = Modules(ModuleName)

    If mdl.Find(StringToFind, lSLine, lSCol, lELine, lECol, FindWholeWord, _
            MatchCase, PatternSearch) = True Then
        If IsMissing(NewString) = False Then
            ' Store text of line containing string.
            sLine = mdl.Lines(lSLine, Abs(lELine - lSLine) + 1)
            ' Determine length of line.
            lLineLen = Len(sLine)
            ' Determine number of characters preceding search text.
            lBefore = lSCol - 1
            ' Determine number of characters following search text.
            lAfter = lLineLen - CInt(lECol - 1)
            ' Store characters to left of search text.
            sLeft = Left$(sLine, lBefore)
            ' Store characters to right of search text.
            sRight = Right$(sLine, lAfter)
            ' Construct string with replacement text.
            sNewLine = sLeft & NewString & sRight
            ' Replace original line.
            mdl.ReplaceLine lSLine, sNewLine
        End If
        SearchOrReplace = True
    Else
        SearchOrReplace = False
    End If

    Set mdl = Nothing
End Function

回答by Oorang

I assume you mean how to do this programatically (otherwise it's just ctrl-h). Unless this is being done in the context of a VBE Add-In, it is rarely (if ever) a good idea. Self modifying code is often flagged by AV software an although access will letyou do it, it's not really robust enough to handle it, and can lead to corruption problems etc. In addition, if you go with self modifying code you are preventing yourself from ever being able to use an MDE or even a project password. In other words, you will never be able to protect your code. It might be better if you let us know what problem you are trying to solve with self modifying code and see if a more reliable solution could be found.

我假设您的意思是如何以编程方式执行此操作(否则它只是 ctrl-h)。除非这是在 VBE 加载项的上下文中完成的,否则很少(如果有的话)是个好主意。自修改代码通常被 AV 软件标记,尽管访问可以您这样做,但它并没有足够强大来处理它,并且可能导致损坏问题等。此外,如果您使用自修改代码,您将阻止自己曾经能够使用 MDE 甚至项目密码。换句话说,您将永远无法保护您的代码。如果您让我们知道您正在尝试使用自我修改代码解决什么问题,并查看是否可以找到更可靠的解决方案,那可能会更好。

回答by Xarxas

additional for the function (looping through all the lines)

附加功能(循环遍历所有行)

Public Function ReplaceWithLine(modulename As String, StringToFind As String, NewString As String)
    Dim mdl As Module
    Set mdl = Modules(modulename)

    For x = 0 To mdl.CountOfLines
     Call SearchOrReplace(modulename, StringToFind, NewString)
    Next x

    Set mdl = Nothing
End Function

Enjoy ^^

欣赏^^

回答by Mark Lavin

Check out the VBA object browser for the Access library. Under the Module object you can search the Module text as well as make replacements. Here is an simple example:

查看 Access 库的 VBA 对象浏览器。在 Module 对象下,您可以搜索 Module 文本以及进行替换。这是一个简单的例子:

In Module1

在模块 1 中

Sub MyFirstSub()
    MsgBox "This is a test"
End Sub

In Module2

在模块 2 中

Sub ChangeTextSub()
    Dim i As Integer

    With Application.Modules("Module1")
        For i = 1 To .CountOfLines
            If InStr(.Lines(i, 1), "This is a Test") > 0 Then
                .ReplaceLine i, "Msgbox ""It worked!"""
            End If
        Next i
    End With
End Sub

After running ChangeTextSub, MyFirstSub should read

运行 ChangeTextSub 后,MyFirstSub 应读取

Sub MyFirstSub()
MsgBox "It worked!"
End Sub

It's a pretty simple search but hopefully that can get you going.

这是一个非常简单的搜索,但希望可以帮助您前进。