• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

How to search for strings in VBA Ccode

How can I use VBA Code to check for the string:
   Const ThisProcName as string = "XXX"
in every procedure of every module of my mdb, and return the name of the first module/ procedure that doesn't have it?
NOTE: 'XXX' is the name of each procedure.
0
Milewskp
Asked:
Milewskp
  • 2
  • 2
2 Solutions
 
Chris BottomleyCommented:
As a starter for 10 check out Chips page here.

http://www.cpearson.com/excel/vbe.aspx

In more specific detail, what are you trying to do ... searh every module and every sub in each module to make sure the string "XXX" is referenced in the subs?  If so I assume you include functions in the scenario?

Chris
0
 
MilewskpAuthor Commented:
hi chris,
<what are you trying to do ... searh every module and every sub in each module to make sure the string "XXX" is referenced in the subs?  If so I assume you include functions in the scenario?>
Yes, every procedure, including subs, functions and event procedures.

0
 
Chris BottomleyCommented:
The following ought to do as requested:

Const kstrFindString As String = "XXX"
 
is teh string you want to see if it is present in each sub/function

findModules is the sub to call with the relevant database being open open and active.

You stated you want to know the first sub without the string so the final line:

    msgbox arr(0)

dose just that ... but the array does contain every sub/function in teh normal code modules where the string was NOT found.  You can process it as required of course as the string identifies the module name and the code routine name.

Note in order to test I added a dummy line in the ListProcedures to avoid false triggers in that sub ... you can of course delete that line if required ... but if you want to retain it then you need to replace XXX in both subs with your real search string.

Chris
Sub findModules()
Dim VBProj As Object
Dim VBComp As Object
Dim arr As Variant
Const kstrFindString As String = "XXX"
    
    ReDim arr(0)
    arr(0) = "NOT found"
    Set VBProj = Application.VBE.ActiveVBProject
    For Each VBComp In VBProj.VBComponents
        Select Case VBComp.Type
            Case 11 'vbext_ct_ActiveXDesigner
                'ActiveX
            Case 2 'vbext_ct_ClassModule
                'Class Module
            Case 100 'vbext_ct_Document
                'Document Module
            Case 3 'vbext_ct_MSForm
                'UserForm
            Case 1 'vbext_ct_StdModule
                'Code Module
                arr = ListProcedures(VBComp.Name, arr, kstrFindString)
            Case Else
                'Unknown Type
        End Select
    Next VBComp
    Debug.Print arr(0)

End Sub

Function ListProcedures(strModuleName As String, arr As Variant, strFindMe) As Variant
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim ProcName As String
Dim ProcKind As Long
Dim lngBodyLine As Long
Dim lngStartLine As Long
Dim lngProcLines As Long
Dim intArraySize As Integer
        
    'XXX - Stop false triggers!
    Set VBProj = Application.VBE.ActiveVBProject
    Set VBComp = VBProj.VBComponents(strModuleName)
    Set CodeMod = VBComp.CodeModule
        
    ProcKind = 0
    intArraySize = UBound(arr)
    With CodeMod
        lngBodyLine = .CountOfDeclarationLines + 1
        Do Until lngBodyLine >= .CountOfLines
            ProcName = .ProcOfLine(lngBodyLine, ProcKind)
            lngStartLine = .ProcStartLine(ProcName, ProcKind)
            lngBodyLine = .ProcBodyLine(ProcName, ProcKind)
            lngProcLines = .ProcCountLines(ProcName, ProcKind)
            lngStartLine = lngStartLine + lngProcLines + 1
            If Not .Find(strFindMe, lngBodyLine, 1, lngBodyLine + lngProcLines, -1, True, False) Then
                ReDim Preserve arr(0 To intArraySize)
                arr(intArraySize) = strModuleName & ":" & ProcName
                intArraySize = intArraySize + 1
            End If
            lngBodyLine = lngStartLine
        Loop
    End With
    ListProcedures = arr

End Function

Open in new window

0
 
MilewskpAuthor Commented:
Thanks Chris,
That's what I was looking for. Great link too!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now