在 VBA 中评估用户窗体是模式的还是无模式的

Evaluate if a Userform is modal or modeless in VBA

在 VBA (Excel) 中有没有办法确定用户表单的 属性 是模态的还是非模态的?

我以为我可以评估属性 ShowModal 但显然 bolFormState = UserForm1.ShowModal 不起作用(它在 UserForm1.ShowModal method or data member not found 上发出错误消息)。

简单的解决方法

如果您只想在用户窗体显示期间询问(不更改模式),您可以在调用代码中使用 (public) 变量从 ShowModal 属性 的不同行为中获益。

解释:

由于只有无模式调用会立即执行最后一个赋值 bModal = False,因此可以在 UserForm 实例的 运行-time

期间请求 bModal 变量

调用代码示例

Option Explicit
Public bModal

Sub zeigeUF()
Dim info As clsModal
bModal = True           ' provide for default (vbModal or 1)

With New UserForm1
    .Show vbModeless
End With

'if ShowModal property is set to vbModeless (equals False or 0)
'the following assignment gets executed immediately
'and the variable bModal can be asked for in UserForm
bModal = False             ' visible for Userform only in case of modeless display


End Sub

将以下功能添加到您的用户表单

Private Function isFormModeless() As Boolean

    On Error GoTo EH

    Me.Show vbModeless
    isFormModeless = True

    Exit Function

EH:
    isFormModeless = False

End Function

你可以用

测试它
Private Sub CommandButton1_Click()
    MsgBox "This form is " & IIf(isFormModeless = True, "Modeless", "Modal")
End Sub