VBA自动化错误-“服务器[非服务器应用程序]";在关闭表格时 [英] VBA Automation Error - "server [not server application]" upon closing form

查看:45
本文介绍了VBA自动化错误-“服务器[非服务器应用程序]";在关闭表格时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试以该对象所示的面向对象的方式创建表单: https://stackoverflow.com/a/38382104/4460023 .关闭表单后,我想引用对象属性 IsCancelled 来检查调用子例程是否应该继续执行.但是,当我在表单外部检查此属性时,遇到以下错误:

I've tried creating a form in the object-oriented manner as shown in this answer: https://stackoverflow.com/a/38382104/4460023. Upon closing the form, I'd like to refer to the object property IsCancelled to check if the calling subroutine should continue executing. However, when I check this property outside of the form, I run into the following error:

运行时错误'-2147418105':自动化错误.被调用方(服务器[非服务器应用程序])不可用并消失;全部连接无效.该调用可能已执行."

"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."

我猜想这与表单被关闭有关.作为一种替代解决方案,我只需要写一个存储在调用子模块中的全局变量即可.不过,理想情况下,我想在此form对象中使用该属性.我的代码如下:

I'm guessing this has something to do with the form being closed. As an alternative solution, I simply write to a global variable stored within the calling sub's module. Ideally though, I'd like to use the property within this form object. My code is included below:

以以下形式:

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

请注意,当我不关闭表单时,我可以在类中使用其他字符串属性-只有表单关闭会触发此问题.

Note that I have other string properties within the class that I can use when I do not close the form - it's only the form closing that triggers this problem.

推荐答案

修复代码

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自动化错误-“服务器[非服务器应用程序]";在关闭表格时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆