2017-08-23 51 views
0

我坚持了一些我无法理解的东西(但我知道它可能很简单)。我尝试一些通用的日常收集典型字对象的属性,如表格,文本框,范围等,这里去的想法:VBA将word.table传递给使用对象变量的子程序

Sub GetWobjectAttributes(ByRef WObject As Object) 

    If TypeOf WObject Is Word.Table Then 
      'grab table attributes 
     ElseIf TypeOf WObject Is Word.Range Then 
      'grab range attributes 
     ... 

     End If 
    End Sub 

不幸的是,使用通用的对象类型传递的对象似乎改变的通过型式目的。例如在执行子测试:

Sub test() 


GetWobjectAttributes (ThisDocument.Tables(1)) 

'MsgBox (TypeName(ThisDocument.Range.Tables(1))) 

End Sub 


Public Sub GetWobjectAttributes(ByRef WObject As Variant) 

    MsgBox (TypeName(WObject)) 

End Sub 

显示消息:

范围,而不是 “表格”(但执行MSGBOX(类型名(ThisDocument.Range.Tables(1)))直接在测试子(例如,在评论)显示:?表

任何想法如何通过未知类型的对象,而类型改变子程序

回答

0

的一点是,你CA将Sub作为函数(通过在参数周围添加())。这显然返回“默认成员”,即范围。

调用子像一个子周围的参数去掉(),它都将返回表:

Sub test() 

    GetWobjectAttributes ThisDocument.Tables(1) 

    MsgBox (TypeName(ThisDocument.Range.Tables(1))) 

End Sub 


Public Sub GetWobjectAttributes(ByRef WObject As Variant) 

    MsgBox (TypeName(WObject)) 

End Sub 
+0

谢谢,现在很明显。我有一些大脑褪色:/解决方案完美。 – jareckii