如何在“ mshta.exe”弹出窗口(VBA宏)中插入换行符?

时间:2019-01-18 12:02:18

标签: excel vba

我需要一个不停止宏的msgbox。有没有一种方法可以插入一条换行符,与msgbox的'vbNewLine'相同?

这些作品都不是

Chr(13) 
Chr(10)
vbLf 
vbCr 
vbCrLf 
vbNewLine
"<br>"
Function mshta(ByVal MessageText As String, Optional ByVal Title As String, Optional ByVal PauseTimeSeconds As Integer)
'mshta.exe as an alternative for msgbox

'[...] some other stuff

Dim ConfigString As String
Set WScriptShell = CreateObject("WScript.Shell")

ConfigString = "mshta.exe vbscript:close(CreateObject(""WScript.Shell"")." & "Popup(""" & MessageText & """," & PauseTimeSeconds & ",""" & Title & """))"
WScriptShell.Run ConfigString

End Function

如果我对该函数进行校准:

mshta "Hello<magic?>World"

我希望它显示:

Hello
World

1 个答案:

答案 0 :(得分:1)

这里是一种解决方案,该解决方案支持使用API​​调用而不是WScript.Shell的回车符,并且可以在Excel VBA中使用。它支持标准的枚举参数,例如vbQuestion + vbYesNo,并且可以返回用户响应。如果发生超时,则返回32000

这还具有在与应用程序相同的监视器上而不是在主显示屏上显示弹出窗口的优点。

' This part needs to be at the top of a VBA module
#If Win64 Then 
    Private Declare PtrSafe Function MsgBoxTimeout _
        Lib "user32" _
        Alias "MessageBoxTimeoutA" ( _
            ByVal hwnd As LongPtr, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As VbMsgBoxStyle, _
            ByVal wlange As Long, _
            ByVal dwTimeout As Long) _
    As Long
#Else
    Private Declare Function MsgBoxTimeout _
        Lib "user32" _
        Alias "MessageBoxTimeoutA" ( _
            ByVal hwnd As Long, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As VbMsgBoxStyle, _
            ByVal wlange As Long, _
            ByVal dwTimeout As Long) _
    As Long
#End If


Sub TestMsgbox()
    Dim ReturnValue

    ReturnValue = MsgBoxTimeout(0, "Do you like this message?" & vbCrLf & "This message box will be closed after 4 seconds." & vbCrLf & vbCrLf & "(See Immediate window for return value)", "Return Choice", vbQuestion + vbYesNoCancel, 0, 4000)
    Select Case ReturnValue
        Case vbYes
            Debug.Print "You picked Yes."
        Case vbNo
            Debug.Print "You picked No."
        Case vbCancel
            Debug.Print "You picked Cancel."
        Case 32000
            Debug.Print "Timeout before user made selection."
    End Select
End Sub

更多信息: https://www.extendoffice.com/documents/excel/3836-excel-message-box-timer-timeout.html