我从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.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
为什么我为集合中的第一个项目获取“String”而不是“Document”的任何想法?
答案 0 :(得分:3)
c.Add (d)
与
不同c.Add d
首先,通过将d
括在括号中,您将其作为表达式进行评估,并将该表达式的结果(在本例中为String)添加到集合。在第二个中,添加了d
对象本身。
尝试直接在立即窗口中进行比较:
? TypeName(ActiveDocument) '>> Document
和
? TypeName( (ActiveDocument) ) '>> String