编辑.vbs以接受参数

时间:2015-12-25 04:27:47

标签: vbscript

我要做的是通过包含\href{run:./xx.xls}从LaTeX生成的PDF打开某个工作表上的Excel文件。

我发现下面的VBScript代码(@brettdj)对于打开具有指定文件名的.vbs文件非常有帮助。 但是如何让代码在每次执行时接受一个参数(打开一个不同的文件),而不是在strFileName中指定文件名?

Const xlVisible = -1
Dim objExcel
Dim objWb
Dim objws
Dim strFileName
strFileName = "E:RoomContentsAll.xls"
On Error Resume Next
Set objExcel = CreateObject("excel.application")
Set objWb = objExcel.Workbooks.Open(strFileName)
Set objws = objWb.Sheets(2)
On Error GoTo 0
If Not IsEmpty(objws) Then
    If objws.Visible = xlVisible Then
        objExcel.Goto objws.Range("a1")
    Else
        wscript.echo "the 2nd sheet is present but is hidden"
    End If
    objExcel.Visible = True
Else
    objExcel.Quit
    Set objExcel = Nothing
    If IsEmpty(objWb) Then
        wscript.echo strFileName & " not found"
    Else
        wscript.echo "sheet2 not found"
    End If
End If

1 个答案:

答案 0 :(得分:0)

WScript.Arguments(0)是第一个参数,(1)是第二个,等等

Const xlVisible = -1
Dim objExcel
Dim objWb
Dim objws
Dim strFileName
strFileName = WScript.Arguments(0)
On Error Resume Next
Set objExcel = CreateObject("excel.application")
Set objWb = objExcel.Workbooks.Open(strFileName)
Set objws = objWb.Sheets(2)
On Error GoTo 0
If Not IsEmpty(objws) Then
    If objws.Visible = xlVisible Then
        objExcel.Goto objws.Range("a1")
    Else
        wscript.echo "the 2nd sheet is present but is hidden"
    End If
    objExcel.Visible = True
Else
    objExcel.Quit
    Set objExcel = Nothing
    If IsEmpty(objWb) Then
        wscript.echo strFileName & " not found"
    Else
        wscript.echo "sheet2 not found"
    End If
End If
相关问题