如何搜索 VBA 代码文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10118061/
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-08 12:55:29  来源:igfitidea点击:

How to search through VBA code files

ms-accessvbaaccess-vba

提问by Brandon Moore

I just started a job with a new company where previous developers had created many automated tasks. Of course, there is virtually no documentation and I didn't have a chance to work with the previous developer so now I'm trying to sift through all these processes looking for one that modifies some specific files.

我刚开始在一家新公司工作,以前的开发人员在那里创建了许多自动化任务。当然,几乎没有文档,而且我没有机会与前任开发人员合作,所以现在我正在尝试筛选所有这些过程,以寻找能够修改某些特定文件的过程。

I've scripted all the stored procs in SQL and used a search tool and didn't find what I was looking for, so now I am wondering if the process I need is located in one of many Access databases that are used. With SQL Server, it was easy to write a C# app to script the procs so I could search through them, but with Access it looks like I'm confined to opening each db individually to search through the code files.

我已经在 SQL 中编写了所有存储过程的脚本并使用了搜索工具,但没有找到我要找的东西,所以现在我想知道我需要的进程是否位于使用的许多 Access 数据库之一中。使用 SQL Server,很容易编写一个 C# 应用程序来编写 procs 脚本,以便我可以搜索它们,但是使用 Access 看起来我只能单独打开每个数据库来搜索代码文件。

Is there any way to programatically search through VBA code files?

有没有办法以编程方式搜索 VBA 代码文件?

采纳答案by HansUp

If your interest is searching code modules in an Access database file, you can use the VBE object model. This sample searches for a word in all the modules of the ActiveVBProjectof the current database. If the database includes more than one VBProject, you can enumerate the VBProjects collection and search the projects one at a time by name:

如果您的兴趣是在 Access 数据库文件中搜索代码模块,则可以使用 VBE 对象模型。本示例ActiveVBProject在当前数据库的所有模块中搜索一个词。如果数据库包含多个 VBProject,您可以枚举 VBProjects 集合并按名称一次搜索一个项目:

For Each objComponent In Application.VBE.VBProjects(ProjName).VBComponents

Or if you prefer to reference the project by number rather than name, just be aware the numbering starts with 1 rather than 0.

或者,如果您更喜欢通过编号而不是名称来引用项目,请注意编号以 1 而不是 0 开头。

Public Sub findWordInModules(ByVal pSearchWord As String)
    'Dim objComponent As VBComponent
    ' VBComponent requires reference to Microsoft Visual Basic
    ' for Applications Extensibility; use late binding instead:
    Dim objComponent As Object
    Dim strMessage As String
    Dim strModuleList As String

    strModuleList = vbNullString
    For Each objComponent In Application.VBE.ActiveVBProject.VBComponents
        If objComponent.CodeModule.Find(pSearchWord, 1, 1, -1, -1) = True Then
            strModuleList = strModuleList & "; " & objComponent.Name
        End If
    Next objComponent
    strMessage = "Text '" & pSearchWord & "' found in "
    If Len(strModuleList) > 0 Then
        strMessage = strMessage & "modules: " & Mid(strModuleList, 3)
    Else
        strMessage = strMessage & "no modules"
    End If
    Debug.Print strMessage
End Sub

Review the Access help topic for that Findmethod; you may prefer different options than I used.

查看该Find方法的 Access 帮助主题;您可能更喜欢与我使用的不同的选项。

If you want to target multiple db files and search the modules in each, you could automate this using the OpenDatabasemethod. I'll leave the details of that part up to you.

如果您想定位多个 db 文件并在每个文件中搜索模块,您可以使用该OpenDatabase方法自动执行此操作。我会把那部分的细节留给你。

回答by tahwos

Another option, not previously mentioned - is to just print the Project Code, from the context menu, in the VBA Editor. The steps below, can be modified, to suit applications available, but the result is the same - searchable text.

另一个选项(之前未提及)是仅从 VBA 编辑器的上下文菜单中打印项目代码。可以修改以下步骤以适应可用的应用程序,但结果是相同的 - 可搜索文本。

From the VBA Editor, right click the project name, and click print, from the context menu that appears. Make sure "Code" is checked, and click "OK". The printer can be changed, from the "Setup..." menu, available in the last dialog.

在 VBA 编辑器中,右键单击项目名称,然后从出现的上下文菜单中单击打印。确保选中“代码”,然后单击“确定”。可以从最后一个对话框中可用的“设置...”菜单更改打印机。

Right Click "Print"Check Box "Code"

右击“打印”复选框“代码”

On a side note - stand alone modules, SQL, tables, etc, are available for printing, from the "Database Documenter", in the "Analyze" group, of the "DATABASE TOOLS" tab.

附带说明 - 独立模块、SQL、表等可用于打印,从“数据库文档”,在“数据库工具”选项卡的“分析”组中。

Very useful, for sorting and outlining underlying SQL, of Access queries.

非常有用,用于排序和概述 Access 查询的底层 SQL。

Database Documenter

数据库记录器

回答by ja72

Best to download the free MZ-Toolsfor VBA and use their search/replace function.

最好下载免费的MZ-Toolsfor VBA 并使用它们的搜索/替换功能。

Start FindFind Results

开始查找查找结果

Edit

编辑

MZ-Tools for VBA is no longer available. The paid version works with newer office installations.

MZ-Tools for VBA 不再可用。付费版本适用于较新的办公安装。