带有 NL 的 VBScript 消息框

VBScript msgbox with NL

我有以下代码显示 MsgBox 使用两个环境变量:

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

虽然代码有效,但我希望消息中没有换行符。我已阅读以下讨论这种情况的内容: How to use \n new line in VB msgbox() ...?

然而,当我将 env 变量发送到下面时,它会按字面显示。

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

以防上面的代码不清楚...

env 变量 %Title%%Text% 设置了如下批处理语句中的值:

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

代码读取这些环境变量并将其显示在消息框中。

扩展的环境字符串仍然是一个字符串,因此 VBScript 不会在您未告知它的情况下将其计算为 VBScript 代码。

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

不过,Eval is evil又应该避免。

更好的方法是使用换行符(例如 \n)定义环境变量,然后用实际换行符替换占位符:

x = MsgBox(Replace(Text, "\n", vbNewLine), 4144, Replace(Title, "\n", vbNewLine))