Outlook VBA 宏:指示“请稍候”的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2211642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook VBA Macro: Best way to indicate 'please wait'
提问by monojohnny
What's the best practice for indicating to the user that a Macro is running within Outlook ? The macro can take around 1-30 seconds to complete.
向用户指示宏正在 Outlook 中运行的最佳做法是什么?宏可能需要大约 1-30 秒才能完成。
I want to avoid a modal 'msgbox' popping up before the macro is run, as this can be annoying.
我想避免在运行宏之前弹出模态“msgbox”,因为这可能很烦人。
I would rather avoid the hourglass cursor if possible, and wondered if there was a better way.
如果可能,我宁愿避免使用沙漏光标,并想知道是否有更好的方法。
Is there a way of placing a non-modal 'status' message up, whilst the macro is running?
有没有办法在宏运行时放置非模态“状态”消息?
(The macro I have runs against the currently selected mailItem - and it launched by a button on the Quick Access Toolbar).
(我针对当前选定的邮件项运行的宏 - 它通过快速访问工具栏上的按钮启动)。
采纳答案by Fionnuala
This article(also this) on best practice says use the status bar.
This articleon Outlook says:
Outlook 上的这篇文章说:
Changing the Status Bar
There is no way to change the status bar text in Microsoft Outlook. The status bar is not exposed as it is in other Microsoft Office object models.
更改状态栏
无法在 Microsoft Outlook 中更改状态栏文本。状态栏不像在其他 Microsoft Office 对象模型中那样公开。
Outlook.com provides code for a progress box.
Outlook.com 提供了进度框的代码。
回答by 76mel
Couple of things that string to mind, I am sure other will have ideas as well.
有几件事让人想起,我相信其他人也会有想法。
1.Show a form with a progress bar on it that reports progress or has the progress bar in marque mode if you can't report progress 2.Show a form with a picture box with your favourite animated gif inside(spinny pizza etc.). You can turn off the buttons etc. 3. Use win api to get play with the outlook staus bar
1. 显示一个带有进度条的表单,用于报告进度,或者如果您无法报告进度,则在标记模式下显示进度条 2. 显示一个带有图片框的表单,里面有您最喜欢的动画 gif(旋转披萨等) . 您可以关闭按钮等。 3. 使用 win api 玩 Outlook 状态栏
Not knowing what you are doing in your macro you may have to deal with keeping the form “On top” and pumping async progress into it.
不知道你在宏中做什么,你可能不得不处理保持表单“在顶部”并将异步进度注入其中。
Cheers
干杯
Marcus
马库斯
回答by Greedo
Expanding on @76mel's answer, a nice way to do this is with a non-modal userform. Make something really simple with just a label and caption like this:
扩展@76mel 的答案,一个很好的方法是使用非模态用户表单。只需像这样的标签和标题,就可以使事情变得非常简单:
What I like to do is have the userform set as:
我喜欢做的是将用户表单设置为:
- Non modal (in properties F4, set
ShowModal
to false)- This means you can click outside the status bar and it doesn't stop you.
- I set the
StartupPosition
to0-Manual
andTop
andLeft
to something like 100 so that the Status form appears in the top left corner of the screen (out of the way of any other messages which appear in centre by default)
- 非模态(在 properties 中F4,设置
ShowModal
为 false)- 这意味着您可以在状态栏外单击并且不会阻止您。
- 我将
StartupPosition
to0-Manual
和Top
and设置为Left
100 之类的东西,以便状态表单出现在屏幕的左上角(默认情况下出现在中心的任何其他消息除外)
Set the label's value
to some default text for when the Userform first loads
将标签设置value
为用户表单首次加载时的一些默认文本
Public strStatus As String
Public Const defaultStatus As String = "Default status text" 'set this to whatever you want
Sub statusReporter()
frmStatus.Show
'''
'Your code here
'''
frmStatus.lblStatus = "Step 1"
'...
frmStatus.lblStatus = "Step 2"
'...
'''
'Unload the form
'''
frmStatus.lblStatus = defaultStatus
frmStatus.Hide
End Sub
Note, like with Excel's Application.Statusbar
you must reset the userform to its default value if you plan to use it later on in the same instance of Excel
Optionally use this too
请注意,与 Excel 一样,Application.Statusbar
如果您打算稍后在同一 Excel 实例中使用它,则必须将用户表单重置为其默认值 也可选择使用此值
'Written By RobDog888 - VB/Office Guru?
'Add a Command Button so you can toggle the userform's topmost effect
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" ( _
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
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private mlHwnd As Long
Private Sub UserForm_Initialize()
Dim overTim As Single
overTim = Timer
mlHwnd = FindWindow("ThunderDFrame", "Status") 'Change "Status" to match your userforms caption
Do While mlHwnd = 0 And Timer - overTim < 5
mlHwnd = FindWindow("ThunderDFrame", "Status")
DoEvents
Loop
'Set topmost
SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
in the userform code itself to keep it on top always
在用户表单代码本身中始终保持在顶部