variables - VBScript - 将参数传递给 HTA

标签 variables vbscript hta

我正在 VBScript 中编写一些脚本,我需要将一些变量值传递到 HTA,我将用作前端来显示正在发生更新。

我该怎么做?

VBScript-------

TestVar1 = "Something 1"
TestVar2 = "Something 2"

wshShell.Run "Updater.hta " & TestVar1 & TestVar2

然后

HTA------- 

TestVar1 = Something.Arguments(0)
TestVar2 = Something.Arguments(1)

Msgbox TestVar1
Msgbox TestVar2

我意识到这不是完全正确的代码,我只是放置它来说明我正在尝试做什么。

如果你们能提供解决这个问题的任何帮助,那就太好了,谢谢!

最佳答案

将参数括在引号中。由于 VBScript 使用 " 作为字符串文字,因此您需要通过加倍 "" 来转义它,或者可以使用 Chr() 函数来指定引号字符:

TestVar1 = "Something 1"
TestVar2 = "Something 2"

Dim strParams
strParams = strParams & " " & Chr(34) & TestVar1 & Chr(34)
strParams = strParams & " " & Chr(34) & TestVar2 & Chr(34)

wshShell.Run "updater.hta" & strParams

在您的 HTA 中,Arguments 集合不可用。相反,您必须解析 HTA 对象的 CommandLine 属性。在这种情况下,您的 HTA 收到的 CommandLine 将如下所示:

"updater.hta" "Something 1" "Something 2"

因此,您有两种选择来检索您的参数。您可以使用正则表达式来获取引号内的所有内容,也可以在引号上使用 Split() CommandLine 。如果您的参数之一有引号,事情会变得更加棘手,您可能需要考虑使用不同的字符来括住您的参数。

这是一个使用 Split() 提取参数的 HTA 框架:

<head>
    <HTA:APPLICATION
        ID="myhta" 
        APPLICATIONNAME="HTA Test"
    >
</head>

<script language="VBScript">
    Sub Window_OnLoad()
        a = Split(myhta.CommandLine, Chr(34))
        MsgBox "Arg 1 = " & a(3)
        MsgBox "Arg 2 = " & a(5)
    End Sub
</script>

当您使用Split()时,您将得到如下内容:

a = Split(myhta.CommandLine, Chr(34))
' a(0) = <blank>
' a(1) = "updater.hta"
' a(2) = " "
' a(3) = "Something 1"
' a(4) = " "
' a(5) = "Something 2"
' a(6) = <blank>

因此,a(3) 成为您的第一个参数,a(5) 成为您的第二个参数。

如果你想使用正则表达式,它会变成:

Sub Window_OnLoad()

    With New RegExp
        .Global = True
        .Pattern = """([^""]+)"""
        Set c = .Execute(myhta.CommandLine)
    End With

    For i = 1 To c.Count - 1        ' Skip i = 0 (the HTA filename)
        MsgBox "Arg " & i & " = " & c(i).SubMatches(0)
    Next

End Sub

这将显示:

Arg 1 = Something 1
Arg 2 = Something 2

关于variables - VBScript - 将参数传递给 HTA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31773996/

相关文章:

vba - HTA 和查找函数 Excel VBA

python - 如何将文本小部件内容设置为 Python/Tkinter 中的变量值?

variables - 在范围循环中递增结构变量

c - C 中的值是 double 的

java - GC 和在 for 循环中赋值

postgresql - 如何创建一个具有有限访问权限、无插入更新但可以执行重新索引和备份等维护任务的 postgres 角色?

javascript - 如何用脚本改变一个像素

sql-server - 用于读取文本文件并执行查询的 VB 脚本

activex - 连接到 Acrobat 的 IAC(应用程序间通信)

batch-file - 如何将退出状态从 HTA (vbscript) 返回到调用批处理文件