VB獲取對象成員,包括常量、方法、屬性等。orm
使用的 TypeLib Information庫,通常系統中都有,若是找不到能夠百度一下"tlbinf32.dll",下載下來註冊就好了。對象
一個簡單的Demo事件
Option Explicitip
Private objCurrent As Objectci
Private Sub Command1_Click()
Call AddListItem(Text1.Text, List1)
End Subget
Private Sub AddListItem(objName As String, lst As ListBox)
On Error GoTo err_exit:
Dim i As Long
Dim oTLB As InterfaceInfo
Dim obj As Object
Set objCurrent = Nothing
Set obj = CreateObject(objName)
Set objCurrent = obj
Set oTLB = TLI.InterfaceInfoFromObject(obj)it
lst.Cleario
For i = 1 To oTLB.Members.Count
Select Case oTLB.Members(i).InvokeKind
Case TLI.INVOKE_CONST '常量
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_EVENTFUNC '事件
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_FUNC '方法
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_PROPERTYGET 'get屬性
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_PROPERTYPUT 'let屬性
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_PROPERTYPUTREF 'set屬性
lst.AddItem oTLB.Members(i).Name
Case TLI.INVOKE_UNKNOWN '未知
lst.AddItem oTLB.Members(i).Name
End Selectform
Next i
err_exit:
End Sub百度
Private Function GetHelpString(obj As Object, strMember As String) As String
' If obj = Nothing Then
' GetHelpString = ""
' Exit Function
' End
Dim i As Long
Dim oTLB As InterfaceInfo
Set oTLB = TLI.InterfaceInfoFromObject(obj)
For i = 1 To oTLB.Members.Count
If oTLB.Members(i).Name = strMember Then
GetHelpString = oTLB.Members(i).HelpString
Exit Function
End If
Next i
End Function
Private Sub List1_Click()
List1.ToolTipText = GetHelpString(objCurrent, List1.Text)
Text2.Text = GetHelpString(objCurrent, List1.Text)
End Sub
在輸入框中輸入要建立對象的完整類名,如「scripting.filesystemobject」,點擊按鈕,便可遍歷對象中的成員,顯示在列表中,選中某個成員,提示該成員的幫助信息。