从VBA显示超时值的消息框的最佳方式是什么? [英] Whats the best way to display a message box with a timeout value from VBA?

查看:261
本文介绍了从VBA显示超时值的消息框的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于以下列出的最佳解决方法,也可能是另一个您知道的解决方法。

This question is regarding the best workaround available from those listed below, or perhaps another one that you know of.

这是问题的背景,它来自于代码这个...

This is the question's background it comes from code like this...

Set scriptshell = CreateObject("wscript.shell")
    Const TIMEOUT_IN_SECS = 60
    Select Case scriptshell.popup("Yes or No? leaving this window for 1 min is the same as clicking Yes.", TIMEOUT_IN_SECS, "popup window", vbYesNo + vbQuestion)
        Case vbYes
            Call MethodFoo
        Case -1
            Call MethodFoo
    End Select

这是这是一个简单的方法来显示一个从VBA或VB6超时的消息框。
问题在于,特别是在Excel 2007中(显然也会在Internet Explorer中发生)弹出窗口将不会超时,而是等待用户输入。这个问题很难调试,因为它只会偶尔发生,我不知道重现问题的步骤。到目前为止,我认为这是Office模式对话框的一个问题,并且没有意识到超时已经过期。

看到这里...
http://social.technet.microsoft.com/Forums/en-US/ ITCG / thread / 251143a6-e4ea-4359-b821-34877ddf91fb /

which is a simple way to display a messagebox with a timeout from VBA or VB6 for that matter. The problem is that in Excel 2007 in particular (apparently also happens in Internet Explorer at times) the popup window will unexpectantly NOT timeout, and instead wait for user input. This issue is quite tough to debug as it only happens occasionally and I do not know the steps to reproduce the issue. So far I believe it to be an issue with Office modal dialogs and excel not recognising the timeout has expired.
See here... http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/251143a6-e4ea-4359-b821-34877ddf91fb/

我发现这个问题的解决方法在此列出。

A.使用Win32 API调用

The workarounds to this issue that I have found are listed here.
A. Use a Win32 API call

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 Sub MsgBoxDelay()
    Const cmsg As String = "Yes or No? leaving this window for 1 min is the same as clicking Yes."
    Const cTitle As String = "popup window"
    Dim retval As Long
    retval = MessageBoxTimeout(FindWindow(vbNullString, Title), cmsg, cTitle, 4, 0, 60000)

    If retval <> 7 Then
        Call MethodFoo
    End If

End Sub  

B。使用一个手动计时器与一个设计为看起来像一个消息框的VBA userform使用全局变量或者类似的,以保存需要传递回调用代码的任何状态。确保使用提供的vbModeless参数调用userform的Show方法。

B. Use a manual timer with a VBA userform that is designed to look like a messagebox. Use a global variable or similar to save any state that needs to be passed back to the calling code. Ensure that the Show method of the userform is called with the vbModeless parameter supplied.

C。在MSHTA进程中将呼叫转换为wscript.popup方法,这将允许代码用完程序,避免办公室的模态性。

C. Wrap the call to wscript.popup method in the MSHTA process which would allow the code to run out of process and avoid the modal nature of office.

CreateObject("WScript.Shell").Run "mshta.exe vbscript:close(CreateObject(""WScript.Shell"").Popup(""Test"",2,""Real%20Time%20Status%20Message""))"

这个问题是关于上面列出的最佳解决方法,或者另一个你知道的。那么在VBA中显示超时值的消息框的最佳方法是什么?提名A,B或C或您自己的答案。

This question is regarding the best workaround available from those listed above, or perhaps another one that you know of. So what is the best way to display a messagebox with a timeout value in VBA? Nominate A, B or C or your own answer.

推荐答案

使用答案A. Win32解决方案。这符合要求,并且从迄今为止的测试中可靠。

Going with Answer A. the Win32 solution. This meets the requirements, and is robust from testing so far.

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 Sub MsgBoxDelay() 
    Const cmsg As String = "Yes or No? leaving this window for 1 min is the same as clicking Yes." 
    Const cTitle As String = "popup window" 
    Dim retval As Long 
    retval = MessageBoxTimeout(FindWindow(vbNullString, Title), cmsg, cTitle, 4, 0, 60000) 

    If retval <> 7 Then 
        Call MethodFoo 
    End If 

End Sub

这篇关于从VBA显示超时值的消息框的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆