【问题标题】:How to insert a break line in 'mshta.exe' pop up (VBA macro)?如何在“mshta.exe”弹出窗口(VBA 宏)中插入换行符?
【发布时间】:2021-03-16 17:36:02
【问题描述】:

我需要一个不会停止宏的 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

【问题讨论】:

标签: excel vba


【解决方案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

【讨论】:

  • 嗨,Ben,这很有趣,我以前没听说过!不幸的是,它没有做我想做的事。当消息显示在屏幕上时,我需要代码继续运行。我赞成您的答案,但未标记为正确答案。感谢您的努力!
  • 我做了很多搜索,但找不到 mshta 支持回车的方法。对于 Excel,您可能需要创建一个带有计时器事件的用户窗体来显示消息并允许代码继续运行。
  • 谢谢你,Ben,真遗憾,我希望有一个替代方案。老实说,我根本不喜欢用户表单的想法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多