使用VBA替换MS Access中的模块文本 [英] Replace Module Text in MS Access using VBA

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

问题描述

如何从Access中的另一个模块中搜索和替换Access中的模块中的文本?我在Google上找不到此内容.

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.

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

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

调用DoCmd.DeleteObject(acModule,modBase64)

Call DoCmd.DeleteObject(acModule, modBase64)

推荐答案

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

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

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

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