我尝试以面向对象的方式创建表单,如以下答案所示:https://stackoverflow.com/a/38382104/4460023 。关闭表单后,我想引用对象属性 IsCancelled
来检查调用子例程是否应继续执行。但是,当我在表单之外检查此属性时,遇到以下错误:
"Run-time error '-2147418105': Automation error. The callee (server [not server application]) is not available and disappeared; all connections are invalid. The call may have executed."
我猜这与关闭表单有关。作为替代解决方案,我只需写入存储在调用子模块中的全局变量即可。但理想情况下,我想使用此表单对象中的属性。我的代码如下:
在表单内:
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
关于关闭表单时出现 VBA 自动化错误 - "server [not server application]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51179449/