typename 产生意外结果

1 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.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
Run Code Online (Sandbox Code Playgroud)

我从中得到以下输出:

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
Run Code Online (Sandbox Code Playgroud)

我期望得到的是:

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
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么我为集合中的第一项获得“字符串”而不是“文档”?

Tim*_*ams 5

c.Add (d) 
Run Code Online (Sandbox Code Playgroud)

不等于

c.Add d 
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,通过用d括号括起来,可以将其计算为表达式,并将该表达式的结果(在本例中为字符串)添加到集合中。在第二个中,d添加对象本身。

尝试直接在立即窗口中进行比较:

? TypeName(ActiveDocument)       '>> Document
Run Code Online (Sandbox Code Playgroud)

? TypeName( (ActiveDocument) )   '>> String
Run Code Online (Sandbox Code Playgroud)