2

我有一个带有命令按钮的 vb 脚本和 excel 页面。

vb脚本---test.vbs

MsgBox("Hello world")

excel vba代码

Private Sub CommandButton1_Click()
  Dim SFilename As String
    SFilename = "C:\Users\mkamaraj\Desktop\test.vbs" 'Change the file path

    ' Run VBScript file
    Set wshShell = CreateObject("Wscript.Shell")
    wshShell.Run """" & SFilename & """"
End Sub

当我单击 Excel 中的按钮时,它会执行VBScriptMessageBox显示 。现在,我需要将TextBox Excel VBAto传递,VBScript并且该值应该与 that 一起显示VBScript MessagBox

我怎样才能做到这一点?

4

3 回答 3

3

您可以将参数发送到 VBScript。看看下面的链接:

我可以将参数传递给 VBScript(使用 cscript 启动的 vbs 文件)吗?

VB脚本:

MsgBox("Hello " & WScript.Arguments(0))

VBA:

Private Sub CommandButton1_Click()
  Dim SFilename As String
    SFilename = "C:\Users\mkamaraj\Desktop\test.vbs " & """Something Else""" 'Change the file path

    ' Run VBScript file
    Set wshShell = CreateObject("Wscript.Shell")
    wshShell.Run """" & SFilename & """"
End Sub
于 2013-08-22T10:37:08.853 回答
0

处理未命名参数的简单测试脚本 (showparms.vbs):

Option Explicit

Function qq(s)
  qq = """" & s & """"
End Function

Function Coll2Arr(oColl, nUB)
  ReDim aTmp(nUB)
  Dim i : i = 0
  Dim e
  For Each e In oColl
      aTmp(i) = e
      i       = i + 1
  Next
  Coll2Arr = aTmp
End Function

Dim oWAU  : Set oWAU = WScript.Arguments.Unnamed
Dim aWAU  : aWAU     = Coll2Arr(oWAU, oWAU.Count - 1)
Dim sArgs : sArgs    = "no arguments given"
If -1 < UBound(aWAU) Then
    sArgs = qq(Join(aWAU, """ """))
End If
MsgBox sArgs ' WScript.Echo sArgs

一个简单的 VBA 子程序,用于调用带有未命名参数(包含空格)的 .VBS:

Option Explicit

Sub callVBS()
  Dim sFSpec As String: sFSpec = "p:\ath\to\showparms.vbs"
  Dim sParms As String: sParms = "one ""t w o"" three"
  Dim sCmd As String: sCmd = """" & sFSpec & """ " & sParms
  Dim oWSH: Set oWSH = CreateObject("WScript.Shell")
  oWSH.Run sCmd
End Sub
于 2013-08-22T10:34:13.743 回答
0

我发现这是直截了当的,简单的,更少的双引号。
需要记住在字符串命令中传递“your.vbs arg1 arg2”之间的空格
并且您不需要用双引号封装每个 arg。

sCmd = "\\your\VBS\file\path\selectArgs.vbs"
arg1 = "ThisArg1"
arg2 = "ThisArg2"
sRun = sCmd & " " & arg1 & " " & arg2
Dim wsh As Object
Set wsh = CreateObject("Wscript.Shell")
wsh.Run "" & sRun & ""

'.Run will look like this:
'wsh.Run ""\\your\VBS\file\path\selectArgs.vbs ThisArg1 ThisArg2""
于 2021-03-09T21:31:29.177 回答