2015-12-25 40 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