-3

I have the following code that displays a MsgBox using two env variables:

Set wshShell = CreateObject("WScript.Shell")
Title = wshShell.ExpandEnvironmentStrings("%Title%")
Text = wshShell.ExpandEnvironmentStrings("%Text%")
x = MsgBox(Text, 4144, Title)

Whilst the code works I wish t have a new line character in the message. I have read the following which discusses this scenario: How to use \n new line in VB msgbox() ...?

However when I sen the env variable to the following it is displayed literally.

"This is the first line" & vbCrLf & "and this is the second line"

Just in case the code above is unclear...

The env variables %Title% and %Text% are set with values like in these batch statements:

set Title="This is a title"
set Text="This is the first line" & vbCrLf & "and this is the second line"

The code reads and displays these env variables in a message box.

Ansgar Wiechers
  • 184,186
  • 23
  • 230
  • 299
Dercni
  • 1,094
  • 2
  • 13
  • 33

1 Answers1

1

The expanded environment string is still a string, so VBScript doesn't evaluate it as VBScript code without you telling it to do so.

x = MsgBox(Eval(Text), 4144, Eval(Title))

However, Eval is evil and should be avoided.

A better approach would be to define your environment variables using a placeholder for the newlines (e.g. \n) and then replace the placeholders with actual newlines:

x = MsgBox(Replace(Text, "\n", vbNewLine), 4144, Replace(Title, "\n", vbNewLine))
Ansgar Wiechers
  • 184,186
  • 23
  • 230
  • 299