【问题标题】:Outlook 2013 VBA How to Display Modeless Dialog BoxOutlook 2013 VBA 如何显示无模式对话框
【发布时间】:2020-06-24 20:57:48
【问题描述】:

我有每次收到新电子邮件时都会运行的 VBA 代码。它有几个处理步骤,包括创建 Excel 电子表格,因此可能需要一两分钟才能执行。

我想显示一个无模式对话框,在处理电子邮件时显示更新的状态消息。我创建了一个 UserForm1,但不知道如何从 VBA 代码中实例化它。

【问题讨论】:

标签: vba outlook


【解决方案1】:

像这样:

Dim uf As UserForm1
Set uf = New UserForm1
uf.Show False

但是,这不是一个好的做法,因为通知应该是模态的。也许您想要SystemModal(在所有窗口前面)而不是ApplicationModal(在应用程序前面)? VBA MsgBox 实际上可以完全自定义,请查看this post here to learn more on how to customize a MsgBox

使窗口置顶

如果您希望表单窗口成为 TopMost,请尝试以下操作:

https://support.microsoft.com/en-us/kb/184297

  Option Explicit
  Public Const SWP_NOMOVE = 2
  Public Const SWP_NOSIZE = 1
  Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
  Public Const HWND_TOPMOST = -1
  Public Const HWND_NOTOPMOST = -2

  Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos"  _
        (ByVal hwnd As Long, _
        ByVal hWndInsertAfter As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        ByVal cx As Long, _
        ByVal cy As Long, _
        ByVal wFlags As Long  ) As Long

  Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _
     As Long

     If Topmost = True Then 'Make the window topmost
        SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _
           0, FLAGS)
     Else
        SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _
           0, 0,FLAGS)
        SetTopMostWindow = False
     End If
  End Function

要在您的表单上使用它:

 res = SetTopMostWindow(uf.hwnd, True)

【讨论】:

  • 谢谢。这行得通。我确实希望消息出现在所有窗口的前面,但是应用程序必须在无人值守的情况下运行,所以我相信这意味着它需要是无模式的。
  • 谢谢,这是一个很好的参考。但对于可能遇到此问题的其他人,请注意这适用于 VB。要在 Outlook VBA 中使用需要稍作修改。
【解决方案2】:

在收到来自@AnalystCave.com 的信息后,测试代码的最终版本如下。仍在研究如何强制用户窗体停留在其他窗口之上。

Public Sub TestForm()

  Dim uf As UserForm1
  Set uf = New UserForm1

  Load uf

  uf.Show vbModeless
  uf.msgStatus.Text = "11111111111111111"
  uf.msgStatus.Text = "22222222222222222"
  uf.msgStatus.Text = "33333333333333333"
  uf.Hide

  Unload uf

End Sub

在我的特定应用程序中,桌面上显示的内容是可以预测的。在这种情况下,解决问题的一种方法是:

''' execute something that you know will show on top of the userform then

uf.Show vbModeless      ' this will put the userform back on top

【讨论】:

  • 查看我的更新答案。使用SetWindowTopMost函数
猜你喜欢
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多