我有VBA代码,每次收到新的电子邮件时都会运行。它有几个处理步骤,包括创建excel电子表格,因此可能需要一两分钟才能执行。
我想显示一个无模式对话框,在电子邮件处理过程中显示更新的状态消息。我创建了一个UserForm1,但无法弄清楚如何从VBA代码中实例化它。
答案 0 :(得分:3)
像这样:
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。
如果您希望Form窗口为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)
答案 1 :(得分:0)
从@ 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