我尝试以面向对象的方式创建表单,如这个答案所示:https://stackoverflow.com/a/38382104/4460023 https://stackoverflow.com/a/38382104/4460023。关闭表单后,我想引用对象属性IsCancelled
检查调用子例程是否应继续执行。但是,当我在表单之外检查此属性时,遇到以下错误:
“运行时错误'-2147418105':自动化错误。被调用者(服务器
[不是服务器应用程序])不可用并消失;全部
连接无效。该调用可能已执行。”
我猜这与关闭表单有关。作为替代解决方案,我只需写入存储在调用子模块中的全局变量即可。但理想情况下,我想使用此表单对象中的属性。我的代码如下:
表格内:
Private cancelling As Boolean
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelling
End Property
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
cancelling = True
End If
End Sub
然后在调用子例程中:
Set frm = New ViewByWorkerForm
frm.Show
If frm.IsCancelled Then 'error happens here
Exit Sub
End If
请注意,当我不关闭表单时,我可以在类中使用其他字符串属性 - 只有表单关闭才会触发此问题。
修复您的代码
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
End If
Hide
cancelling = True
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)