将参数从 vba 传递到 vbs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18376588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:54:00  来源:igfitidea点击:

passing argument from vba to vbs

vbavbscript

提问by Pinky

I have one vb script and excel page with command button.

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

vb script---test.vbs

vb 脚本---test.vbs

MsgBox("Hello world")

excel vba code

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

When I click the button in Excel it executes the VBScriptand the MessageBoxis displayed. Now, I need to pass the TextBoxvaluefrom Excel VBAto VBScriptand that value should be displayed with that VBScriptMessagBox.

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

How can I do that?

我怎样才能做到这一点?

采纳答案by Mark

You can send Parameters to the VBScript. Have a look at the link below:

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

Can I pass an argument to a VBScript (vbs file launched with cscript)?

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

VBScript:

脚本:

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

VBA:

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

回答by Ekkehard.Horner

A simple test script to deal with unnamed arguments (showparms.vbs):

处理未命名参数的简单测试脚本 (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

A simple VBA sub to call a .VBS with unnamed arguments (containing spaces):

使用未命名参数(包含空格)调用 .VBS 的简单 VBA 子程序:

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