将参数从 VBS 传递到 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13876338/
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 18:48:37  来源:igfitidea点击:

Pass Argument from VBS to VBA

vbavbscriptargument-passing

提问by Kay

I try to call a VBA subroutine from VBS with passing a string variable from VBS to VBA, but can't find the appropiate syntax:

我尝试从 VBS 调用 VBA 子例程,并将字符串变量从 VBS 传递到 VBA,但找不到合适的语法:

'VBS:
'------------------------
Option Explicit

Set ArgObj = WScript.Arguments 
Dim strPath

mystr = ArgObj(0) '?

'Creating shell object 
Set WshShell = CreateObject("WScript.Shell")

'Creating File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Getting the Folder Object
Set ObjFolder = objFSO.GetFolder(WshShell.CurrentDirectory)

'Getting the list of Files
Set ObjFiles = ObjFolder.Files

'Creat a Word application object
Set wdApp = CreateObject("Word.Application")
wdApp.DisplayAlerts = True
wdApp.Visible = True

'Running macro on each wdS-File
Counter = 0
For Each objFile in objFiles
  If UCase(objFSO.GetExtensionName(objFile.name)) = "DOC" Then
    set wdDoc = wdApp.Documents.Open(ObjFolder & "\" & ObjFile.Name, 0, False) 
    wdApp.Run "'C:\Dokumente und Einstellungen\kcichini\Anwendungsdaten\Microsoft\Word\STARTUP\MyVBA.dot'!Test_VBA_with_VBS_Args" (mystr) 'how to pass Argument???
    Counter = Counter + 1
  End if
Next

MsgBox "Macro was applied to " & Counter & " wd-Files from current directory!"

wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing



'------------------------
'VBA:
'------------------------
Sub Test_VBA_with_VBS_Args()

    Dim wdDoc As Word.Document
    Set wdDoc = ActiveDocument
    Dim filename As String
    Dim mystr As String

    'mystr = how to recognize VBS-Argument ???

    filename = ActiveDocument.name
    MsgBox "..The file: " & filename & " was opened and the VBS-Argument: " & mystr & "recognized!" 

    wdDoc.Close

End Sub
'------------------------

回答by

You need to specify parameters in your VBA Suband use them as you would do if using it from VBA normally.

您需要在 VBA 中指定参数Sub并使用它们,就像正常从 VBA 使用它一样。

For example, I tried the following VBScript

例如,我尝试了以下 VBScript

dim wd: set wd = GetObject(,"Word.Application")
wd.Visible = true
wd.run "test", "an argument"

and the VBA

和 VBA

Sub Test(t As String)
    MsgBox t
End Sub

which worked successfully, generating a message box.

成功运行,生成了一个消息框。

回答by junglejim63

Addendum to @user69820 answer, if arguments are VBScript variables, they need to be cast as appropriate type before calling the subroutine:

@user69820 答案的附录,如果参数是 VBScript 变量,则需要在调用子例程之前将它们转换为适当的类型:

This does not work:

这不起作用:

dim argumentVariable
argumentVariable = "an argument"
wd.run "test", argumentVariable

This does:

这样做:

dim argumentVariable
argumentVariable = "an argument"
wd.run "test", CStr(argumentVariable)

Tested on Excel 2010, Win7SP1 x64

在 Excel 2010、Win7SP1 x64 上测试