【问题标题】:VBScript msgbox with NL [closed]带有 NL 的 VBScript msgbox [关闭]
【发布时间】:2018-09-18 10:27:54
【问题描述】:

我有以下代码使用两个环境变量显示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"

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

环境变量%Title%%Text% 设置为这些批处理语句中的值:

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

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

【问题讨论】:

标签: vbscript


【解决方案1】:

扩展的环境字符串仍然是一个字符串,因此 VBScript 不会在没有您告诉它的情况下将其评估为 VBScript 代码。

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

但是,Eval is evil 应该避免使用。

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多