关闭表单时出现 VBA 自动化错误 - "server [not server application]"

标签 vba excel

我尝试以面向对象的方式创建表单,如以下答案所示: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/

相关文章:

java - 为什么 Selenium WebDriver 不会切换到我的新选项卡(Excel 报告)

vba - 向形状/线条添加渐变

.net - 如何从 powershell 打开 Excel 工作簿以实现自动化

python - 在 Excel 工作表的开头插入标题

vba - Excel VBA - 循环遍历目录中的文件时,如何跳到下一个文件 onError

excel - 在excel中复制一列并将每个条目/单元格加倍。如何?

excel - 如何防止粘贴到多列中?

excel - 同时将 2 个选定的相邻单元格的内容复制到表格的最后一行

excel - 自动筛选日期时间值 - 本地小数点问题

excel - 使用格式和布局将多个表从 Access DB 导出到 Excel 工作簿