Vba 来自typename的意外结果
我从typename得到了一些意想不到的结果,我被难住了。希望有人能给我指出正确的方向Vba 来自typename的意外结果,vba,ms-word,typename,Vba,Ms Word,Typename,我从typename得到了一些意想不到的结果,我被难住了。希望有人能给我指出正确的方向 Private Sub T() Dim d As Word.Document Dim s As String Dim c As Collection Dim i As Long Dim o As Object Set d = ActiveDocument s = "X" Set c = New Collection Debug.Pri
Private Sub T()
Dim d As Word.Document
Dim s As String
Dim c As Collection
Dim i As Long
Dim o As Object
Set d = ActiveDocument
s = "X"
Set c = New Collection
Debug.Print "d is a " & TypeName(d)
Debug.Print "s is a " & TypeName(s)
Debug.Print "c is a " & TypeName(c)
c.Add (d)
c.Add (s)
For i = 1 To c.count
Debug.Print "Item " & i & " of the collection is a " & " " & TypeName(c.Item(i))
Next i
End Sub
从中我得到以下输出:
d is a Document
s is a String
c is a Collection
Item 1 of the collection is a String
Item 2 of the collection is a String
我期望得到的是:
d is a Document
s is a String
c is a Collection
Item 1 of the collection is a Document
Item 2 of the collection is a String
你知道为什么我在收藏的第一件物品上用“字符串”而不是“文档”吗
c.Add (d)
不一样
c.Add d
在第一种情况下,通过将d
括在括号中,可以将其作为表达式进行计算,并将该表达式的结果(在本例中为字符串)添加到集合中。在第二个示例中,添加了d
对象本身
尝试直接在即时窗口中进行比较:
? TypeName(ActiveDocument) '>> Document
及
非常感谢。我不知道我是否会明白这一点。我甚至数不清有多少次我负责帮助,每次都会想出同样的主意。再次感谢,没问题。标记这些问题类型的一种方法是,当VBEditor在关键字(例如,方法名称,如Add)和左括号之间添加或保留空格时,要小心-如果方法调用需要括号,则括号中将没有空格(如果放置了括号,编辑器将删除它…)
? TypeName( (ActiveDocument) ) '>> String