Outlook 2013 VBA 如何显示无模式对话框

Outlook 2013 VBA How to Display Modeless Dialog Box

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

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

像这样:

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

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

制作Window TopMost

如果你想让你的表格 window 成为最热门的,试试这个:

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)

从@AnalystCave.com 收到信息后的测试代码的最终版本如下。仍在研究如何强制用户窗体保持在另一个之上 windows。

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