2

I'm successfully showing a message box from a batch file using this method given by boflynn for a previous question.

I'm trying to insert a carriage return into the message box body text but all my attempts and combinations of quote marks and Char(13) have thus far failed.

With reference to the above linked answer, I'm looking for a messagbox that would put text on multiple lines, such as:

This will
be shown
in a popup

Is this possible?

Community
  • 1
  • 1
Smartbuild
  • 119
  • 3
  • 15
  • 1
    Hello. Can you show us what you have already tried? – Victor Moraes Aug 08 '16 at 12:03
  • Not VBA but this is closest dup so far - [display a 2 row message pop -up vba 6](http://stackoverflow.com/a/5005241/692942). The code is identical, but like I said it's VBA not VBScript. – user692942 Aug 08 '16 at 13:18
  • 1
    It's just string concatenation, the reason `Char(13)` didn't work is because the function is called `Chr()`, so `Chr(13)` would have worked. Incidentally the [built-in named constants](https://msdn.microsoft.com/en-us/library/hh277t8e(v=vs.84).aspx) already cover the various scenarios so worth using them - `vbCr` is `Chr(13)`, `vbLf` is `Chr(10)`, `vbCrLf` is `Chr(13) & Chr(10)` and `vbNewLine` a Platform-specific newline character; whatever is appropriate for the platform. – user692942 Aug 08 '16 at 13:26
  • Lankymart, thanks for your input - if I had a penny for every time I've type Char instead of Chr..... – Smartbuild Aug 08 '16 at 14:11

2 Answers2

3

VBScript has a builtin constant vbCr for the Carriage Return character. Concatenate your (sub)strings with that constant and display the message box with the result:

MsgBox "This will" & vbCr & "be shown" & vbCr & "in a popup"

To display multiline text from a batch file you need to pass each line as a separate argument

cscript MessageBox.vbs "This will" "be shown" "in a popup"

and concatenate the arguments

ReDim args(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
  args(i) = WScript.Arguments(i)
Next

MsgBox Join(args, vbCr)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

You can try this sample example to call the MsgBox from a function :

@echo off
set "Msg=Hey !\nHere is a message !\nThis will\n be shown\n in popup\n with multi-lines !"
Rem 64=vbInformation, 48=vbExclamation, 16=vbCritical 32=vbQuestion
set Type=64 48 16 32
Set "Title=Example of MsgBox in batch with vbscript"
For %%a in (%Type%) Do Call:MsgBox "%Msg%" "%%a" "%Title%"
exit /b
::**********************************************************
:MsgBox <Msg> <Type> <Title>
echo MsgBox Replace("%~1","\n",vbCrLf),"%~2","%~3" > "%tmp%\%~n0.vbs"
Cscript /nologo "%tmp%\%~n0.vbs" & Del "%tmp%\%~n0.vbs"
exit /b
::**********************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70