excel vba使用命名参数调用vbscript

excel vba使用命名参数调用vbscript,vba,vbscript,Vba,Vbscript,我试图从Excel VBA宏调用VBScript文件。VBScript具有命名参数 我的VBScript如下所示 dim argument1 argument1 = WScript.Arguments.Named.Item("argument1") if argument1 = "" then WScript.Echo "No filepath provided. Aborting." WScript.Quit end

我试图从Excel VBA宏调用VBScript文件。VBScript具有命名参数

我的VBScript如下所示

dim argument1
argument1 = WScript.Arguments.Named.Item("argument1")

if argument1 = "" then
    WScript.Echo "No filepath provided. Aborting."
    WScript.Quit
end if

WScript.Echo "Launching Excel..."
dim excelInstance
set excelInstance = CreateObject("Excel.Application")

dim Application
set Application = excelInstance.Application

excelInstance.Visible = false
Application.ScreenUpdating = false

WScript.Echo "Opening Excel file..."
dim File1 
set File1 = OpenWorkbook(excelInstance, argument1)
Sub CreateOutput()
    Dim filePath, fileDir, shellCommand, vbScriptPath
    fileDir = ThisWorkbook.Path
    filePath = fileDir & "\exampleFile.xlsx"
    vbScriptPath = fileDir & "\VBScript1.vbs"
    shellCommand = vbScriptPath & " " & Chr(34) & filePath & Chr(34)
    
    Set wshShell = CreateObject("Wscript.Shell")
    wshShell.Run """" & shellCommand & """"
    
End Sub
我的Excel VBA宏如下所示

dim argument1
argument1 = WScript.Arguments.Named.Item("argument1")

if argument1 = "" then
    WScript.Echo "No filepath provided. Aborting."
    WScript.Quit
end if

WScript.Echo "Launching Excel..."
dim excelInstance
set excelInstance = CreateObject("Excel.Application")

dim Application
set Application = excelInstance.Application

excelInstance.Visible = false
Application.ScreenUpdating = false

WScript.Echo "Opening Excel file..."
dim File1 
set File1 = OpenWorkbook(excelInstance, argument1)
Sub CreateOutput()
    Dim filePath, fileDir, shellCommand, vbScriptPath
    fileDir = ThisWorkbook.Path
    filePath = fileDir & "\exampleFile.xlsx"
    vbScriptPath = fileDir & "\VBScript1.vbs"
    shellCommand = vbScriptPath & " " & Chr(34) & filePath & Chr(34)
    
    Set wshShell = CreateObject("Wscript.Shell")
    wshShell.Run """" & shellCommand & """"
    
End Sub
在执行宏时,我得到一个弹出窗口,显示消息“未提供文件路径。正在中止”
有关如何将exampleFile.xlsx的路径作为命名参数传递的任何建议?

请以这种方式尝试VBA子项:

Sub CreateOutput()
    Dim filePath, fileDir, shellCommand, vbScriptPath
    fileDir = ThisWorkbook.path
    filePath = fileDir & "\exampleFile.xlsx"
    vbScriptPath = fileDir & "\VBScript1.vbs"
    
    shellCommand = "cscript """ & vbScriptPath & """ """ & filePath & """"
    
    Shell shellCommand, vbHide
End Sub
已编辑

VBScript代码应如下所示:

dim argument1

if WScript.Arguments.Count = 0 then
    WScript.Echo "No filepath provided. Aborting."
    WScript.Quit
end if
argument1= WScript.Arguments(0) 

'msgbox argument1
msgbox "Launching Excel..."
dim excelInstance
set excelInstance = CreateObject("Excel.Application")

excelInstance.Visible = true 'you may make it false after seeing it working.
'Application.ScreenUpdating = false
msgbox "Opening Excel file..."
dim File1 
set File1 = excelInstance.Workbooks.Open(argument1)

请以这种方式尝试VBA接头,请:

Sub CreateOutput()
    Dim filePath, fileDir, shellCommand, vbScriptPath
    fileDir = ThisWorkbook.path
    filePath = fileDir & "\exampleFile.xlsx"
    vbScriptPath = fileDir & "\VBScript1.vbs"
    
    shellCommand = "cscript """ & vbScriptPath & """ """ & filePath & """"
    
    Shell shellCommand, vbHide
End Sub
已编辑

VBScript代码应如下所示:

dim argument1

if WScript.Arguments.Count = 0 then
    WScript.Echo "No filepath provided. Aborting."
    WScript.Quit
end if
argument1= WScript.Arguments(0) 

'msgbox argument1
msgbox "Launching Excel..."
dim excelInstance
set excelInstance = CreateObject("Excel.Application")

excelInstance.Visible = true 'you may make it false after seeing it working.
'Application.ScreenUpdating = false
msgbox "Opening Excel file..."
dim File1 
set File1 = excelInstance.Workbooks.Open(argument1)

使用命名参数调用:

Sub-CreateOutput()
Dim filePath、fileDir、SHELL命令、vbScriptPath、wshShell
fileDir=ThisWorkbook.Path
filePath=fileDir&“\exampleFile.xlsx”
vbScriptPath=fileDir&“\VBScript1.vbs”
shellCommand=“”&vbScriptPath&“/argument1:”&filePath&“
设置wshShell=CreateObject(“Wscript.Shell”)
运行shell命令
端接头

使用命名参数调用:

Sub-CreateOutput()
Dim filePath、fileDir、SHELL命令、vbScriptPath、wshShell
fileDir=ThisWorkbook.Path
filePath=fileDir&“\exampleFile.xlsx”
vbScriptPath=fileDir&“\VBScript1.vbs”
shellCommand=“”&vbScriptPath&“/argument1:”&filePath&“
设置wshShell=CreateObject(“Wscript.Shell”)
运行shell命令
端接头

我认为VBA应该以“argument:parameter”的形式传递参数,即prameter的名称应该在命令行参数中,否则vbscript无法识别参数。可能更清楚的是,您将名称argument1更改为“myFilePath”,并传递myFilePath:停止询问已经讨论过多次的问题,只需搜索即可。我认为VBA应将参数传递为“argument:parameter”,即参数的名称应在命令行参数中,否则vbscript无法识别参数。可能更清楚的是,您将名称argument1更改为“myFilePath”并传递myFilePath:停止询问已经讨论过多次的问题,只需搜索,您就会知道。我实现了上述建议并执行了宏。我还使用下面的代码“File1.Range(“I2”)=“Temp1”对我的VBScript测试了宏。exampleFile.xlsx文件未打开或编辑。@busy bee:最好的测试方法是查看工作簿是否打开。实际上,您已经完全更改了OPs代码,不使用命名参数。当调用VBScript时,OP只需传递
/argument:value
,代码就可以工作了。@Lankymart:是的,我这样做了。因为他只需要通过一个论点,而且他看起来没有经验,所以我试着选择最简单的方式。在VBA中,也使用
Shell
VBA函数。您也可以通过这种方式发送许多参数。生成一个字符串并使用字符分隔符,然后将其拆分以提取必要的参数。实现您的建议后,我的代码将正确执行。谢谢。我执行了上述建议并执行了宏。我还使用下面的代码“File1.Range(“I2”)=“Temp1”对我的VBScript测试了宏。exampleFile.xlsx文件未打开或编辑。@busy bee:最好的测试方法是查看工作簿是否打开。实际上,您已经完全更改了OPs代码,不使用命名参数。当调用VBScript时,OP只需传递
/argument:value
,代码就可以工作了。@Lankymart:是的,我这样做了。因为他只需要通过一个论点,而且他看起来没有经验,所以我试着选择最简单的方式。在VBA中,也使用
Shell
VBA函数。您也可以通过这种方式发送许多参数。生成一个字符串并使用字符分隔符,然后将其拆分以提取必要的参数。实现您的建议后,我的代码将正确执行。谢谢,是的,是的。