Iterating through Object Browser in VBA

I would like to iterate over elements of any class in a reference library, as is done using the object browser. How can this be done using VBA?

+3
source share
5 answers

I found KB from Microsoft , which allowed me to do just that. It also covers iteration over the details of the participants.

Private Sub ListClassesInAccess() Dim TypeLibrary As TypeLibInfo Dim ClassList As CoClasses Dim i As Integer Dim Path As String Path = "C:\Program Files\Microsoft Office\OFFICE11\MSACC.OLB" Set TypeLibrary = TypeLibInfoFromFile(Path) Set ClassList = TypeLibrary.CoClasses For i = 1 To ClassList.Count MsgBox ClassList.Item(i).Name Next Set TypeLibrary = Nothing Set ClassList = Nothing End Sub 
+3
source

Actually, how to do this is undocumented, but possible. If you want to implement syntax for each syntax for a collection, you can do the following:

 Option Compare Database Option Explicit Public colT As New Collection Public Function NewEnum() As IUnknown Set NewEnum = colT.[_NewEnum] End Function Public Property Get NextItem() As IUnknown Attribute NextItem.VB_UserMemId = -4 Attribute NextItem.VB_MemberFlags = "40" Set NextItem = colT.[_NewEnum] End Property 

Note the attribute settings above. You should use SaveAsText and edit the code as indicated above in notepad. Then you re-import the code using loadfromText on the debug command line. Once you do this, you can go:

 Dim n As clstest1 Dim v As Variant Set n = New clstest1 [ code here that adds to collection] For Each v In n Debug.Print v Next 

And if you do not want to use for each ... for the collection, you can / can also set the default property for the class by going:

 Public Property Get Item(Optional ndx As Integer = 1) As Variant Attribute Item.VB_UserMemId = 0 Select Case ndx Case 1: Item = Me.s1 Case 2: Item = Me.s2 Case 3: Item = Me.s3 End Select End Property Public Property Get count() As Integer count = 3 End Property 

Then you can go:

 Dim n As clstest1 Dim i As Integer Set n = New clstest1 For i = 1 To n.count Debug.Print n(i) Next 

However, I do not know how you can automatically add each method / member of the class to the built-in automatic assembly of objects (there is no way to serialize this with compiler parameters, but I saw the code with each procedure with the attribute Item.VB_UserMemId = 1, then 2, then 3 ) Maybe someone with a lot of knowledge can jump over).

However, as shown above, you can implement for..each for collections. And you can implement an index for each of the properties / methods if you create your own element property. And, as shown above, you can even set this property of the element that you create as the default value. I added "optional" and therefore even:

 debug.print n 

Will work, or

 debug.print n.Item(1) 
+9
source

Unfortunately, Access VBA does not support reflection. You can try to create your own abstraction of the hierarchy of objects, which will check the values โ€‹โ€‹of properties, etc. For you. You can start with something like this:

http://msdn.microsoft.com/en-us/library/aa663065%28office.11%29.aspx

+2
source

If you have VB6 installed, you can try tlbinf32.dll. AFAIR - if there are a number of classes for information on any type library See http://support.microsoft.com/kb/224331

+1
source
+1
source

All Articles