【问题标题】:VBA Automation Error - "server [not server application]" upon closing formVBA自动化错误-关闭表单时出现“服务器[不是服务器应用程序]”
【发布时间】:2018-07-04 18:43:08
【问题描述】:

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

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

【问题讨论】:

  • 看看here怎么做

标签: vba excel


【解决方案1】:

修复你的代码

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        Cancel = True
    End If
    Hide
    cancelling = True
End Sub

【讨论】:

  • 绝妙的答案!
【解决方案2】:

对我来说,我没有“红色 x”(我把它隐藏了),我不想使用布尔值。我通过一个事件这样调用我的用户表单:

Public Sub UserForm_Sub()

'Application.ScreenUpdating = False

Set UserFormUserForm = New UserFormUserForm
On Error GoTo ErrorHandler
UserFormUserForm.ShowUserFormUserForm

GoTo Continue
ErrorHandler:
ModelessFormShowing = False
Continue:

End Sub

在我的例子中,使用 Excel 的错误处理程序是一件简单的事情。您可能会找到On Error Resume Next,但在我的情况下,如果在启动期间出现错误,我需要将ModelessFormShowing 布尔值设置为 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2017-09-18
    相关资源
    最近更新 更多