VB获取对象成员,包括常量、方法、属性等。
使用的 TypeLib Information库,一般系统中都有,如果找不到可以百度一下"tlbinf32.dll",下载下来注册就行了。
一个简单的Demo
1 Option Explicit 2 3 Private objCurrent As Object 4 5 Private Sub Command1_Click() 6 Call AddListItem(Text1.Text, List1) 7 End Sub 8 9 Private Sub AddListItem(objName As String, lst As ListBox) 10 On Error GoTo err_exit: 11 Dim i As Long 12 Dim oTLB As InterfaceInfo 13 Dim obj As Object 14 Set objCurrent = Nothing 15 Set obj = CreateObject(objName) 16 Set objCurrent = obj 17 Set oTLB = TLI.InterfaceInfoFromObject(obj) 18 19 lst.Clear 20 21 For i = 1 To oTLB.Members.Count 22 Select Case oTLB.Members(i).InvokeKind 23 Case TLI.INVOKE_CONST '常量 24 lst.AddItem oTLB.Members(i).Name 25 Case TLI.INVOKE_EVENTFUNC '事件 26 lst.AddItem oTLB.Members(i).Name 27 Case TLI.INVOKE_FUNC '方法 28 lst.AddItem oTLB.Members(i).Name 29 Case TLI.INVOKE_PROPERTYGET 'get属性 30 lst.AddItem oTLB.Members(i).Name 31 Case TLI.INVOKE_PROPERTYPUT 'let属性 32 lst.AddItem oTLB.Members(i).Name 33 Case TLI.INVOKE_PROPERTYPUTREF 'set属性 34 lst.AddItem oTLB.Members(i).Name 35 Case TLI.INVOKE_UNKNOWN '未知 36 lst.AddItem oTLB.Members(i).Name 37 End Select 38 39 Next i 40 err_exit: 41 End Sub 42 43 Private Function GetHelpString(obj As Object, strMember As String) As String 44 ' If obj = Nothing Then 45 ' GetHelpString = "" 46 ' Exit Function 47 ' End 48 Dim i As Long 49 Dim oTLB As InterfaceInfo 50 Set oTLB = TLI.InterfaceInfoFromObject(obj) 51 52 For i = 1 To oTLB.Members.Count 53 If oTLB.Members(i).Name = strMember Then 54 GetHelpString = oTLB.Members(i).HelpString 55 Exit Function 56 End If 57 Next i 58 End Function 59 60 Private Sub List1_Click() 61 List1.ToolTipText = GetHelpString(objCurrent, List1.Text) 62 63 Text2.Text = GetHelpString(objCurrent, List1.Text) 64 End Sub
在输入框中输入要创建对象的完整类名,如“scripting.filesystemobject”,点击按钮,即可遍历对象中的成员,显示在列表中,选中某个成员,提示该成员的帮助信息。