G
Greg Maxey
Hi,
As part of a larger personal project, I am trying to build a list of
each VB project loaded in a current document and a list of the
individual modules and procedures in those projects.
Using the code shown below, I have been able to get the names of all
loaded projects and the individual modules/procedures of the active
template and open documents.
I am having a problem with the Add-Ins. I am beginning to believe
that there may not be a VBA solution when I discovered the protection
property of the project is "Read" only. Still I thought I would ask
here.
A secondary question has to do with my code in general. While I don't
have as much exasperation these days working with arrays, I am still
not very confident or efficient. While the process I have written is
working as I expected I don't know if it is sound or correct. I would
appreaciate some feedback. What I have done is simple monkey see,
monkey do. This monkey would really like to finally understand this
concept of variants, arrays, an array of variants, etc.
I remember either Jonathan West, JGM or Tony Jolans tried to explain
it to me one time, but I can't locate that exchange. If someone would
take this code and try to educate me again I will appreciate it.
Thanks.
Option Explicit
Sub ScratchMacro()
Dim i As Long
Dim j As Long
Dim pProjName As String
Dim vProjects() As String
Dim oComp As VBComponent
Dim ListArray As Variant
For i = 0 To Application.VBE.VBProjects.Count - 1
ReDim vProjects(Application.VBE.VBProjects.Count)
On Error GoTo Err_Handler1
pProjName = Application.VBE.VBProjects.Item(i + 1).FileName
pProjName = Right(pProjName, Len(pProjName) - InStrRev(pProjName,
"\"))
vProjects(i) = pProjName
For Each oComp In Application.VBE.VBProjects.Item(i +
1).VBComponents
If oComp.Type = vbext_ct_StdModule Or oComp.Type =
vbext_ct_Document Then
ListArray = ListMacros(oComp)
For j = 0 To UBound(ListArray) - 1
MsgBox oComp.Name & ": " & ListArray(j)
Next j
End If
Next oComp
ReEntry:
Next
Exit Sub
Err_Handler1:
If Err.Number = 50289 Then
'Any way to access the project components that are in loaded Addins?
'Application.VBE.VBProjects.Item(i + 1).Protection = False
Resume
End If
Resume ReEntry
End Sub
Public Function ListMacros(oModule As VBComponent) As Variant
'Set reference to the VBA Extensibility library
Dim pString As String
Dim lngLineCount As Long
Dim lngModLevelLines As Long
Dim pName As String
pString = ""
lngModLevelLines = oModule.CodeModule.CountOfDeclarationLines
If lngModLevelLines > 0 Then
lngLineCount = lngModLevelLines + 1
Else
lngLineCount = 1
End If
Do While lngLineCount <= oModule.CodeModule.CountOfLines
pName = oModule.CodeModule.ProcOfLine(lngLineCount, vbext_pk_Proc)
pString = pString & pName & ","
lngLineCount = lngLineCount +
oModule.CodeModule.ProcCountLines(pName, vbext_pk_Proc)
Loop
ListMacros = Split(pString, ",")
End Function
As part of a larger personal project, I am trying to build a list of
each VB project loaded in a current document and a list of the
individual modules and procedures in those projects.
Using the code shown below, I have been able to get the names of all
loaded projects and the individual modules/procedures of the active
template and open documents.
I am having a problem with the Add-Ins. I am beginning to believe
that there may not be a VBA solution when I discovered the protection
property of the project is "Read" only. Still I thought I would ask
here.
A secondary question has to do with my code in general. While I don't
have as much exasperation these days working with arrays, I am still
not very confident or efficient. While the process I have written is
working as I expected I don't know if it is sound or correct. I would
appreaciate some feedback. What I have done is simple monkey see,
monkey do. This monkey would really like to finally understand this
concept of variants, arrays, an array of variants, etc.
I remember either Jonathan West, JGM or Tony Jolans tried to explain
it to me one time, but I can't locate that exchange. If someone would
take this code and try to educate me again I will appreciate it.
Thanks.
Option Explicit
Sub ScratchMacro()
Dim i As Long
Dim j As Long
Dim pProjName As String
Dim vProjects() As String
Dim oComp As VBComponent
Dim ListArray As Variant
For i = 0 To Application.VBE.VBProjects.Count - 1
ReDim vProjects(Application.VBE.VBProjects.Count)
On Error GoTo Err_Handler1
pProjName = Application.VBE.VBProjects.Item(i + 1).FileName
pProjName = Right(pProjName, Len(pProjName) - InStrRev(pProjName,
"\"))
vProjects(i) = pProjName
For Each oComp In Application.VBE.VBProjects.Item(i +
1).VBComponents
If oComp.Type = vbext_ct_StdModule Or oComp.Type =
vbext_ct_Document Then
ListArray = ListMacros(oComp)
For j = 0 To UBound(ListArray) - 1
MsgBox oComp.Name & ": " & ListArray(j)
Next j
End If
Next oComp
ReEntry:
Next
Exit Sub
Err_Handler1:
If Err.Number = 50289 Then
'Any way to access the project components that are in loaded Addins?
'Application.VBE.VBProjects.Item(i + 1).Protection = False
Resume
End If
Resume ReEntry
End Sub
Public Function ListMacros(oModule As VBComponent) As Variant
'Set reference to the VBA Extensibility library
Dim pString As String
Dim lngLineCount As Long
Dim lngModLevelLines As Long
Dim pName As String
pString = ""
lngModLevelLines = oModule.CodeModule.CountOfDeclarationLines
If lngModLevelLines > 0 Then
lngLineCount = lngModLevelLines + 1
Else
lngLineCount = 1
End If
Do While lngLineCount <= oModule.CodeModule.CountOfLines
pName = oModule.CodeModule.ProcOfLine(lngLineCount, vbext_pk_Proc)
pString = pString & pName & ","
lngLineCount = lngLineCount +
oModule.CodeModule.ProcCountLines(pName, vbext_pk_Proc)
Loop
ListMacros = Split(pString, ",")
End Function