0

Sorry but I was trying to find the answer for hours but could not figure it out. I tried playing with vbNewLine and vbCrLf but could not make it to work in the function and in the function call.

How do I add a new line with the code below?

Tried this but it did not work:

checker = MessageTimeOut("Underlying raw data in the workbook has been updated." & vbNewLine & "This will close automatically.", "UPDATE RAW DATA - COMPLETED", 5) 

Also tried:

checker = MessageTimeOut("Underlying raw data in the workbook has been updated." & vbCrLf & "This will close automatically.", "UPDATE RAW DATA - COMPLETED", 5)

I want the "This will close automatically." shown in a new line.

Function MessageTimeOut(str_message As String, str_title As String, int_seconds As Integer) As Boolean
    Dim Shell
    Set Shell = CreateObject("WScript.Shell")
    Shell.Run "mshta.exe vbscript:close(CreateObject(""WScript.shell"").Popup(""" & str_message & """," & int_seconds & ",""" & str_title & """))"
    MessageTimeOut = True
End Function


Sub Some_Sub()
    ' some lengthy code....
    Dim checker As Boolean
    checker = MessageTimeOut("Underlying raw data in the workbook has been updated. This will close automatically.", "UPDATE RAW DATA - COMPLETED", 5)
XYZKLM
  • 137
  • 1
  • 1
  • 8
  • 2
    I tested with removing the message variable and like so: `Popup(""" & "Test Line 1" & vbCrLf & "Test Line 2" & """," & int_seconds & ",""" & str_title & """))"` with no success - I also tested passing a variable from your sub to the function `Dim myMessage As String` `MessageTimeOut(myMessage,...)` and could see the function receives the formatting but the popup won't recognise it. I also tried hardcoding into the message other new line things I found on google like `n with no luck - this looks to perhaps be more of a powershell/VBS issue than to do with your VBA... – Samuel Everson May 12 '20 at 12:30
  • It might be worth changing your tags to better suit the issue. – Samuel Everson May 12 '20 at 12:31
  • 2
    Also [this SO question is identifying and asking the same issue as yours](https://stackoverflow.com/questions/54253640/how-to-insert-a-break-line-in-mshta-exe-pop-up-vba-macro) - might be worth looking into. – Samuel Everson May 12 '20 at 12:32
  • 1
    Does this answer your question? [How to insert a break line in 'mshta.exe' pop up (VBA macro)?](https://stackoverflow.com/questions/54253640/how-to-insert-a-break-line-in-mshta-exe-pop-up-vba-macro) – Geert Bellekens May 12 '20 at 16:44
  • Thanks Samuel and Geart. The post you shared was not actually solved. I am looking into Tim's answer below. – XYZKLM May 13 '20 at 04:59
  • With Tim's solution below, I think we can just play around with just vbLf. – XYZKLM May 13 '20 at 05:37

1 Answers1

1

EDIT: My previous answer wasn't using mshta which I think you needed in order to make your message asynchronous and allow your code to continue...

This does the trick:

Sub Test2()

    mshta "Me`s`s`age", "test", 5 '<<< all backticks become newlines

    Debug.Print "This runs right away"

End Sub


Function mshta(ByVal MessageText As String, Optional ByVal Title As String, _
            Optional ByVal PauseTimeSeconds As Integer)

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

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

End Function
Tim Williams
  • 154,628
  • 8
  • 97
  • 125