'ClassName :ParaseTier '缺陷没有考虑错误处理 Public Event onError() '根据字符串得到具体的属性值 PublicFunction GetAttributeValue(ObjectAsObject, ByVal AttributeName AsString) GetAttributeValue = VBA.Interaction.CallByName(GetObject(Object, AttributeName), Trim(AttributeName), VbGet) End Function '根据字符串得到具体的对象 'AttributeIsObject = 0,表示当AttributeName表示的是属性名称 'AttributeIsObject = 1,表示当AttributeName表示的是对象名称 PublicFunctionGetObject(ByVal ObjectAsObject, ByRef AtrributeName AsString, Optional AttributeIsObject =0) AsObject Dim parseProcName() AsString parseProcName =Split(AtrributeName, ".") Dim i AsInteger SetGetObject=Object For i =0ToUBound(parseProcName) -1 If IsCollectionAttribute(parseProcName(i)) Then SetGetObject= GetItemObject(GetObject, parseProcName(i)) Else IfIsObject(VBA.Interaction.CallByName(GetObject, parseProcName(i), VbGet)) Then SetGetObject= VBA.Interaction.CallByName(GetObject, parseProcName(i), VbGet) EndIf EndIf Next '处理需要单独返回对象的属性 If AttributeIsObject =1Then IfIsObject(VBA.Interaction.CallByName(GetObject, parseProcName(0), VbGet)) Then SetGetObject= VBA.Interaction.CallByName(GetObject, parseProcName(0), VbGet) EndIf EndIf AtrributeName = parseProcName(UBound(parseProcName)) Erase parseProcName End Function '解析集合类对象 '用来解释如“Sections(1)”格式的集合对象 '要求集合对象必须包含Item方法 '字符串不允许包含类似Item(1)的方法 PublicFunction GetItemObject(ByVal ObjectAsObject, ByVal AttributeName AsString) AsObject Dim parseProcName() AsString parseProcName =Split(AttributeName, "(") AttributeName =Trim(parseProcName(0)) Dim Index AsInteger Index =Trim(Replace(parseProcName(1), ")", "")) Set GetItemObject =GetObject(Object, AttributeName, 1) Set GetItemObject = GetItemObject.Item(Index) Erase parseProcName End Function '判断当前的对象是否为集合对象 PrivateFunction IsCollectionAttribute(ByVal AttributeName AsString) AsBoolean IsCollectionAttribute = (InStr(1, AttributeName, "(") >0) End Function
相关测试类:
'ClassName :Student Public Name AsString Public Sex AsString
测试模块:
PublicSub Test1() Dim pt AsNew ParaseTier Dim o AsObject Set o = Word.Application.ActiveDocument 'Demo 使用字符串获得属性 Debug.Print pt.GetAttributeValue(o, "Paragraphs(1).Range.Font.Name") 'Demo 使用字符串获得集合对象属性 Debug.Print pt.GetItemObject(o, "Paragraphs(1)").Range.Font.Name 'Demo 使用字符串获得对象 Debug.Print pt.GetObject(o, "Paragraphs", 1).Count Set o =Nothing Set pt =Nothing End Sub PublicSub Test2() Dim pt AsNew ParaseTier Dim o AsObject Set o = Word.Application.ActiveDocument 'Demo 使用字符串获得属性 Debug.Print pt.GetAttributeValue(o, "Paragraphs(1).Range.Font.Name") 'Demo 使用字符串获得集合对象属性 Debug.Print pt.GetItemObject(o, "Sections(1)").Index 'Demo 使用字符串获得对象 Debug.Print pt.GetObject(o, "Paragraphs", 1).Count Set o =Nothing Set pt =Nothing End Sub PublicSub test3() Dim s AsNew Student s.Name ="Duiker" s.Sex ="男" Dim ss AsString ss =InputBox("请输入需要获得的属性名称", "Name") SelectCase ss Case"Name" Debug.Print s.Name Case"Sex" Debug.Print s.Sex EndSelect Set s =Nothing End Sub PublicSub test4() Dim s AsNew Student s.Name ="Duiker" s.Sex ="男" Dim ss AsString ss =InputBox("请输入需要获得的属性名称", "Name") Dim pt AsNew ParaseTier Debug.Print pt.GetAttributeValue(s, ss) Set s =Nothing End Sub