代码之家  ›  专栏  ›  技术社区  ›  Greg Finzer

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

  •  5
  • Greg Finzer  · 技术社区  · 17 年前

    仅供参考,我想出了如何通过编程删除模块:

    调用DoCmd.DeleteObject(acModule,modBase64)

    4 回复  |  直到 14 年前
        1
  •  2
  •   Oorang    17 年前

    我想你指的是如何通过编程实现这一点(否则就是ctrl-h)。除非这是在VBE外接程序的上下文中完成的,否则这几乎不是一个好主意。自动修改代码通常由AV软件标记,尽管访问将 允许 如果你这么做了,它就没有足够的健壮性来处理它,并且可能会导致腐败问题等。此外,如果你使用自修改代码,你将阻止自己使用MDE甚至项目密码。换句话说,您将永远无法保护您的代码。如果您让我们知道您试图用自修改代码解决什么问题,并看看是否可以找到更可靠的解决方案,这可能会更好。

        2
  •  2
  •   Greg Finzer    17 年前

    经过大量搜索,我找到了以下代码:

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '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
    
        3
  •  0
  •   Mark Lavin    17 年前

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

    在模块1中

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

    在模块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
    

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

        4
  •  0
  •   Xarxas    16 年前

    功能的附加功能(在所有行中循环)

    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
    

    享受^^