如何使用 VBA 关闭当前打开的 MsgBox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1139251/
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
How do I close a currently opened MsgBox using VBA?
提问by Jay Riggs
Is there any option to close the currently opened MsgBox using any code in VBA access form application?
是否有任何选项可以使用 VBA 访问表单应用程序中的任何代码关闭当前打开的 MsgBox?
回答by Jay Riggs
Check out Randy Birch's response on thisthread in microsoft.public.vb.general.discussion
在 microsoft.public.vb.general.discussion 中查看 Randy Birch对此线程的回复
He recommends creating a function in a .bas file called MsgBox. Doing so will cause VB to call your function rather than the built in one.
他建议在名为 MsgBox 的 .bas 文件中创建一个函数。这样做会导致 VB 调用您的函数而不是内置函数。
You'd then create your own MsgBox form and build in a timer to close your form after a set period of time. He provides links showing how to do this.
然后,您将创建自己的 MsgBox 表单并内置一个计时器以在设定的时间段后关闭您的表单。他提供了显示如何执行此操作的链接。
He also discusses a way to explicitly call the built in MsgBox function in case you need to do this.
他还讨论了一种在需要时显式调用内置 MsgBox 函数的方法。
Note: I've never done this but I've found that Randy Birch is a knowledgeable resource.
注意:我从未这样做过,但我发现 Randy Birch 是一个知识渊博的资源。
回答by Rob Elliott
MsgBoxes are not intended to be programmatically closed, that's why it's difficult to do so. If you find yourself in a design where you must force close a MsgBox, you should probably re-evaluate your design.
MsgBoxes 不打算以编程方式关闭,这就是为什么很难这样做。如果您发现自己在必须强制关闭 MsgBox 的设计中,您可能应该重新评估您的设计。
回答by Sébastien Nussbaumer
I may be wrong but MsgBox is a blocking call creating a modal form so I don't think there is an easy way such as an option to do that. Do you have a specific use case for this ?
我可能是错的,但 MsgBox 是一个创建模态表单的阻塞调用,所以我认为没有一种简单的方法,例如一个选项来做到这一点。你有具体的用例吗?
回答by onedaywhen
As MarkJ points out, could this could be a dialog generated by Access (rather than a VBA.MsgBox called in your own code)?
正如 MarkJ 指出的那样,这可能是 Access 生成的对话框(而不是在您自己的代码中调用的 VBA.MsgBox)?
For example, when using table's 'dataview' in the Access UI to add a row you get a message, "You are about to append 1 record..." (or similar). Is this the kind of message you mean? If so, there are indeed ways to suppress them...
例如,当在 Access UI 中使用表的“dataview”添加一行时,您会收到一条消息,“您将要追加 1 条记录...”(或类似信息)。这是你说的那种信息吗?如果是这样,确实有办法压制他们……
回答by Nigel Heffernan
I used to have an easy answer to this: use the Windows Scripting Shell object, which has a 'Popup' function - a Message Box, just like the VBA MsgBox() function, with a 'SecondsToWait' parameter that provides exactly the timeout you wanted.
我曾经对此有一个简单的答案:使用 Windows Scripting Shell 对象,它有一个“弹出”功能 - 一个消息框,就像 VBA MsgBox() 函数一样,带有一个“SecondsToWait”参数,可以准确地提供您的超时时间通缉。
With CreateObject("Scripting.WsShell")
.Popup "Watch me disappear in 5 seconds", 5, Application.Name & ": test", vbInformation + vbOkCancel
End With
If you include a 'Cancel' button, it mightstill work: the available parameters are vbOkCancel, vbYesNoCancel, and vbRetryCancel.
如果包含“取消”按钮,它可能仍然有效:可用参数为 vbOkCancel、vbYesNoCancel 和 vbRetryCancel。
If you're trying to close a dialog box you didn't initiate with your own msgBox() function call, that's unhelpful: and, as I've hinted above, the 'SecondsToWait' parameter doesn't really work these days - someone in Redmond really does't like the idea of one thread closing another thread's helpful warnings and important interruptions to the user's workflow.
如果你试图关闭一个对话框,你不是用你自己的 msgBox() 函数调用启动的,那是没有帮助的:而且,正如我上面所暗示的,'SecondsToWait' 参数现在真的不起作用 - 有人在 Redmond 真的不喜欢一个线程关闭另一个线程的有用警告和对用户工作流程的重要中断的想法。
However, you can launch a delayed Message Box 'Close' command using the API Timer() function - not quite 'close the currently opened MsgBox', as this requires advance warning that you intended to open it - but it's the closest thing I have, it fits into a self-contained VBA module, and I posted the code in an answer to a very similar StackOverflow question to yours.
但是,您可以使用 API Timer() 函数启动延迟的 Message Box 'Close' 命令 - 不是完全'关闭当前打开的 MsgBox',因为这需要预先警告您打算打开它 - 但它是我最接近的东西,它适合一个独立的 VBA 模块,我在一个与您的非常相似的 StackOverflow 问题的答案中发布了代码。
I should warn you that the answer in question is using a sledgehammer to crack a nut, with a side order of lengthy explanation.
我应该警告你,有问题的答案是用大锤敲碎坚果,附带冗长的解释。
回答by Crazyd
An easy to use Win32 based answer to actually be useful calling it. Using the below code you can easily do a timer-based PopUpBox. I like 1/2 second timer control so 2 = 1 second. Found this answer looking for the answer to this thread. Above answers don't seem to work. I want to use this when I want to show a quick message or quick answer default is normally right can be used for more.
一个易于使用的基于 Win32 的答案实际上是有用的调用它。使用下面的代码,您可以轻松地创建一个基于计时器的 PopUpBox。我喜欢 1/2 秒的计时器控制,所以 2 = 1 秒。找到这个答案寻找这个线程的答案。以上答案似乎不起作用。当我想显示快速消息或快速回答时,我想使用这个默认值通常是正确的,可以用于更多。
Function Code usually in a Module:
通常在模块中的功能代码:
Declare Function MessageBoxTimeout Lib "user32.dll" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As Long, _
ByVal wLanguageID As Long, _
ByVal lngMilliseconds As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Function PopUpBox(Optional stMessage As String _
= "Yes or No? leaving this window for 1 min is the same as clicking Yes.", _
Optional stTitle As String = "PopUp Window", _
Optional HalfSecTimer As Long = 120, Optional lgVBmsgType As Long = vbYesNo) As Long
Dim RetVal As Long
HalfSecTimer = HalfSecTimer * 500
RetVal = MessageBoxTimeout(FindWindow(vbNullString, Title), stMessage, stTitle, lgVBmsgType, _
0, HalfSecTimer)
PopUpBox = RetVal
End Function
Call Function Code
Examples: Actual code from my database
调用函数代码
示例:来自我的数据库的实际代码
PopUpBox "Re-Linking and Closing dB", "Closing dB", 3, vbOKOnly
intAnswer = PopUpBox("Software Lock Down Active?", "Security", 10, vbYesNo)
回答by Crazyd
I have had a similar problem; theoretically you can use the "SendKeys" function (see http://msdn.microsoft.com/en-us/library/8c6yea83%28VS.85%29.aspx). However, the MsgBox blocks the running so you cannot use the command. If you know when it going to pop up, you may run (from the script) external whshell that wait some time and then use the SendKeys. But if you know how to do that, tell me (here). Other similar possibility is to open different thread/process for it that will not be block, but I don't know if it is possible in VBA.
我也遇到过类似的问题;理论上您可以使用“SendKeys”功能(参见http://msdn.microsoft.com/en-us/library/8c6yea83%28VS.85%29.aspx)。但是,MsgBox 会阻止运行,因此您无法使用该命令。如果您知道它何时会弹出,您可以(从脚本中)运行外部 whshell,等待一段时间然后使用 SendKeys。但如果你知道怎么做,告诉我(在这里)。其他类似的可能性是为其打开不会被阻塞的不同线程/进程,但我不知道在 VBA 中是否可行。
回答by Crazyd
Message Boxes are meant to depict some information to the user. Hence programmatically closing is not a good design, unless you are not automating some process.
消息框旨在向用户描述一些信息。因此,以编程方式关闭不是一个好的设计,除非您没有自动化某些过程。
You can use sendkeys or win APIs.
您可以使用 sendkeys 或 win API。