gpt4 book ai didi

vba - 奇怪的行为 : Variant Set to Object without using Set

转载 作者:行者123 更新时间:2023-12-02 17:40:05 25 4
gpt4 key购买 nike

在将我的答案写到 VBA create IE object from ProcessID 时,我遇到了这种奇怪的行为。请注意,即使 win 变量未设置为 IWebBrowser 对象,它仍然SetIWebBrowser目的。

我是不是漏掉了什么??? Matt、Continium、ThunderFrames...请某人告诉我这个问题吗?

Sub WierdBehavior()
Dim win As Variant
win = CreateObject("Shell.Application").Windows
Debug.Print IsObject(win), TypeName(win)
End Sub

Immediate Window Screenshot

最佳答案

我能够通过让一个类返回一个接口(interface)作为其默认成员来复制该行为。感谢约翰·科尔曼和蒂姆·威廉姆斯给了我这个想法。接口(interface):IClass

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "IClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Public Property Get getFoo() As Object
End Property

类(class):2级

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Class2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Implements IClass
Private MyCollection As New Collection
Private Property Get IClass_getFoo() As Object
Set Class1_getFoo = MyCollection
End Property

Public Function getInterface() As IClass
Attribute getInterface.VB_UserMemId = 0
Set getInterface = Me
End Function

测试

Sub Test()
Dim win As Variant
Dim MyClass2 As New Class2
win = MyClass2
Debug.Print IsObject(win), TypeName(win)
End Sub

结果

Immediate Window Screen Shot

关于vba - 奇怪的行为 : Variant Set to Object without using Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57329490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com