How to search through VBA code files

前端 未结 3 825
生来不讨喜
生来不讨喜 2020-12-25 15:34

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 chan

相关标签:
3条回答
  • 2020-12-25 15:37

    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.

    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.

    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.

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

    0 讨论(0)
  • 2020-12-25 15:58

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

    Start Find

    Edit

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

    0 讨论(0)
  • 2020-12-25 16:01

    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
    

    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
    

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

    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.

    0 讨论(0)
提交回复
热议问题