vba 在不暂停应用程序的情况下使用 MsgBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5459734/
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
Using MsgBox without pausing the application
提问by Patrick Villela
I need to display a message to the user. When I do this using MsgBox
, the program stops until the user clicks the box away. I'd like to know if there's a way to open the MsgBox
without pausing the program.
我需要向用户显示一条消息。当我使用 执行此操作时MsgBox
,程序将停止,直到用户单击该框。我想知道是否有办法在MsgBox
不暂停程序的情况下打开它。
回答by Jean-Fran?ois Corbett
Sounds like you're not expecting any user input from the MsgBox. In this case, depending on your application, the StatusBar
may be an adequate substitute.
听起来您不希望来自 MsgBox 的任何用户输入。在这种情况下,根据您的应用,StatusBar
可能是适当的替代品。
In Excel this is easy:
在 Excel 中,这很容易:
Application.StatusBar = "Please be patient..."
Application.StatusBar = iDone & " of " & iTotal & " items done."
To clear the StatusBar when done:
完成后清除状态栏:
Application.StatusBar = False
In Access, the syntax is a tiny bit more convoluted:
在 Access 中,语法有点复杂:
Temp = SysCmd(acSysCmdSetStatus, "Hey, look at me!") ' Puts out your message
Temp = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
回答by Smandoli
As far as I've ever been able to discover, the answer is you can't. The work-around is a custom form that serves as a dialog box.
据我所知,答案是你不能。解决方法是使用一个用作对话框的自定义窗体。
See http://www.mvps.org/access/forms/frm0046.htm(not precisely your question, but applicable).
请参阅http://www.mvps.org/access/forms/frm0046.htm(不完全是您的问题,但适用)。
回答by ktharsis
MsgBox is modal (meaning the window comes up and halts execution of code until it is cleared). As other posters/commenters have mentioned - your alternative is to write your own version of a popup that is not modal. Not really worth the effort unless you really need it that way.
MsgBox 是模态的(意味着窗口出现并停止执行代码,直到它被清除)。正如其他海报/评论者所提到的 - 您的替代方法是编写自己的非模态弹出窗口版本。除非你真的需要那样做,否则不值得付出努力。
回答by Sam
Create a Form instead. I created a small form that only has a text box that says "Working, Please Wait". When needed I open the form, as a pop-up (docmd openform "form name"), usually just before starting some operation that is going to take some time to complete. When the work completes I close the form (docmd close acform "form name"). This does not stop the program but does provide a "Message" to the user.
而是创建一个表单。我创建了一个只有一个文本框的小表单,上面写着“正在工作,请稍候”。需要时,我通常会在开始一些需要一些时间才能完成的操作之前,将表单作为弹出窗口(docmd openform“表单名称”)打开。工作完成后,我关闭表单(docmd close acform "form name")。这不会停止程序,但会向用户提供“消息”。
回答by user4539187
In the VB editor: Select Insert menu UserForm. In the Toolbox select TextBox: Drag a rectangle in the UserForm and type your text into it. Right click on the UserForm and select Properties. In the ShowModal property: Select False. In your VBA module enter UserForm1.Show where you want to turn it on and UserForm1.Hide where you want to turn it off. UserForm1 is mine, of course use the appropriate name for the form you created.
在 VB 编辑器中: 选择插入菜单用户窗体。在工具箱中选择文本框: 在用户窗体中拖出一个矩形并在其中键入文本。右键单击用户窗体并选择属性。在 ShowModal 属性中:选择 False。在您的 VBA 模块中,输入 UserForm1.Show 您要打开它的位置,输入 UserForm1.Hide 您要关闭它的位置。UserForm1 是我的,当然为您创建的表单使用适当的名称。
回答by HK1
You can use WScript's Popup method. Here's the full details including sample code: http://msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.85%29.aspx
您可以使用 WScript 的 Popup 方法。这是包括示例代码在内的完整详细信息:http: //msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.85%29.aspx
回答by Tiago Cardoso
I believe you first need to evaluate if you really need a msgbox to pops-up and keep you code running.
我相信您首先需要评估您是否真的需要一个 msgbox 来弹出并保持代码运行。
The msgbox functionality (as already stated) is modal and you cannot 'bypass' it. However, you can create a form (similar to the msgbox), set this form as 'not Modal' and call the code to show this form. The code workflow goes on. Tested and works in Excel.
msgbox 功能(如前所述)是模态的,您不能“绕过”它。但是,您可以创建一个表单(类似于 msgbox),将此表单设置为“非模态”并调用代码来显示此表单。代码工作流继续。在 Excel 中测试和工作。
Update: My Access has lost a reference, I won't be able to test it now. Hope it works in Access as well.
更新:我的 Access 丢失了一个参考,我现在无法对其进行测试。希望它也适用于 Access。
Rgds
Rgds