如何通过VBA code文件搜索文件、VBA、code

2023-09-09 21:23:35 作者:薄荷☆糖糖

我刚开始在这里previous开发商创造了许多自动化任务的新公司的工作。当然,几乎没有任何的文件和我没有那么现在我试图通过所有这些过程进行筛选寻找一个修改某些特定的文件与previous开发工作的机会。

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.

我的脚本在SQL存储的所有特效,并使用了搜索工具,并没有发现什么,我一直在寻找的,所以现在我想知道如果我需要的是设在许多Access数据库中所使用的一个过程。使用SQL Server,很容易写一个C#应用程序脚本的特效,所以我可能会通过这些搜索,但访问它看起来像我仅限于打开每个单独的DB通过code文件进行搜索。

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.

有没有办法以编程方式通过VBA code文件中搜索?

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

推荐答案

如果你的兴趣是搜索code模块在一个Access数据库文件,可以使用VBE对象模型。这一个字示例搜索当前数据库中所有的 ActiveVBProject 的模块。如果数据库包括一个以上VBProject,可以列举VBProjects收集和由名称搜索项目一次一个:

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 ActiveVBProject of 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:

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

或者,如果你preFER由数字,而不是名称来引用该项目,只是知道的编号从1开始,而不是0。

Or if you prefer to reference the project by number rather than name, just be aware the numbering starts with 1 rather than 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

查看该查找方法访问帮助主题;你可能preFER不同的选择,比我用。

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

如果你想针对多个数据库文件,然后搜索每个模块,你可以使用的openDatabase 方法自动执行此。我会离开的那部分了细节给你。

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