VB获取对象成员

VB获取对象成员,包括常量、方法、属性等。

使用的 TypeLib Information库,一般系统中都有,如果找不到可以百度一下"tlbinf32.dll",下载下来注册就行了。

一个简单的Demo

 

Option Explicit

Private objCurrent As Object

Private Sub Command1_Click()
Call AddListItem(Text1.Text, List1)
End Sub

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)

lst.Clear

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 Select

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”,点击按钮,即可遍历对象中的成员,显示在列表中,选中某个成员,提示该成员的帮助信息。

posted on 2019-05-03 18:51  带刀侍卫++i  阅读(707)  评论(0编辑  收藏  举报

导航