1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
Sub MacroExists()
Dim MyModule As CodeModule
Dim MyModuleName As String, MySub As String
Dim I As Integer, IndexMatrice As Integer, MyLine As Long
Dim PresenceModule As Boolean, PresenceProcedure As Boolean
Dim Matrice() As Variant
'---------------------------------------------------------------------------
'- test data
MyModuleName = "TestModule"
MySub = "Number2"
'----------------------------------------------------------------------------
On Error Resume Next
'- MODULE
With ActiveDocument.VBProject
PresenceModule = False
If .VBComponents.Count > 0 Then
For I = 1 To .VBComponents.Count
If .VBComponents(I).Name = MyModuleName Then
PresenceModule = True
Set MyModule = .VBComponents(MyModuleName).CodeModule
Exit For
End If
Next I
End If
If PresenceModule = False Then
MsgBox "Module : " & MyModuleName & vbCr & "does not exist.", vbInformation
Exit Sub
End If
End With
'-----------------------------------------------------------------------------
'- SUBROUTINE
'- find first line of subroutine (or error)
IndexMatrice = 0
With MyModule
For I = 1 To .CountOfLines
If Mid(.Lines(I, 1), 1, 3) = "Sub" Then
ReDim Preserve Matrice(2, IndexMatrice)
Matrice(0, IndexMatrice) = Mid(Split(.Lines(I, 1), "(")(0), 5)
Matrice(1, IndexMatrice) = I
Matrice(2, IndexMatrice) = "Sub"
IndexMatrice = IndexMatrice + 1
End If
If Mid(.Lines(I, 1), 1, 8) = "Function" Then
ReDim Preserve Matrice(2, IndexMatrice)
Matrice(0, IndexMatrice) = Mid(Split(.Lines(I, 1), "(")(0), 10)
Matrice(1, IndexMatrice) = I
Matrice(2, IndexMatrice) = "Fonction"
IndexMatrice = IndexMatrice + 1
End If
Next I
End With
If IndexMatrice = 0 Then
MsgBox "Module : " & MyModuleName & " vide.", vbCritical
Exit Sub
End If
PresenceProcedure = False
For IndexMatrice = LBound(Matrice, 2) To UBound(Matrice, 2)
Debug.Print Matrice(0, IndexMatrice)
If Matrice(0, IndexMatrice) = MySub Then
MsgBox "Module : " & MyModuleName & " : " & Matrice(2, IndexMatrice) & " : " & MySub & ", Line Number : " & Matrice(1, IndexMatrice), vbInformation
PresenceProcedure = True
End If
Next IndexMatrice
If PresenceProcedure = False Then
MsgBox "Module exists : " & MyModuleName & " : Sub " & MySub & " does not exist.", vbCritical
End If
Set MyModule = Nothing
End Sub |
Partager