我从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”,有什么想法吗?
发布于 2014-03-05 04:57:51
c.Add (d) 与
c.Add d 在第一个示例中,通过将d封装在括号中,使其被计算为表达式,并将该表达式的结果(在本例中为字符串)添加到集合中。在第二个部分中,添加了d对象本身。
尝试直接在直接窗口中进行比较:
? TypeName(ActiveDocument) '>> Document和
? TypeName( (ActiveDocument) ) '>> Stringhttps://stackoverflow.com/questions/22186853
复制相似问题