2015-12-25 105 views
0

我想要做的是从LaTeX生成的PDF中打开某个工作表上的Excel文件,其中包括\href{run:./xx.xls}编辑.vbs接受参数

我发现下面的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 
+0

您设置'strFileName = WScript.Arguments(0)'然后执行当脚本传递文件名作为第一个命令行参数。见http://ss64.com/vb/arguments.html – Lankymart

回答

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 
+0

我想你错过了[评论](http://stackoverflow.com/questions/34460028/editing-vbs-to-accept-a-parameter#comment56664052_34460028)从6小时前。 – Lankymart

+0

@Ansgar Wiechers 非常感谢。 我有2个其他查询 1-可以打开的文件的位置是相对于vbs文件所在的位置而不是绝对的;作为指定strFileName = WScript.Arguments(。\ subfolder1 \ 0) 2-可以将其他(“objWb.Sheets”中的选项卡名称和“objws.Range”中的单元格引用)指定为参数1和2. – Hany

+0

@brettdj在你的公式中http://stackoverflow.com/questions/8854263/what-the-win-cmd-to-open-a-particular-spreadsheet-in-excel 1- can the location要打开的文件是相对于vbs文件所在的位置而不是绝对的; as指定strFileName = WScript.Arguments(。\ subfolder1 \ 0) 2-可以将其他(“objWb.Sheets”中的选项卡名称和“objws.Range”中的单元格引用)指定为参数1和2. – Hany