2014-03-04 23 views
0

我有一個VBScript我一直在使用刷新我的Excel文件的外部數據,而無需實際打開它:調整一個VBScript來刷新目錄中的所有外部數據Excel文件

Dim oExcel 
Set oExcel = CreateObject("Excel.Application") 

oExcel.Visible = True 
oExcel.DisplayAlerts = False 
oExcel.AskToUpdateLinks = False 
oExcel.AlertBeforeOverwriting = False 

Set oWorkbook = oExcel.Workbooks.Open(FILE NAME GOES HERE) 
oWorkbook.RefreshAll 
oWorkbook.Save 

oExcel.Quit 
Set oWorkbook = Nothing 
Set oExcel = Nothing 

我不知道在哪裏或如何實現一個循環,因此將刷新它是在(即相對路徑)推出該目錄下所有的xlsx和XLS文件

+0

請澄清你的問題之前,所有的數據更新。你如何運行?你到底想要做什麼?當你運行上面的腳本時會發生什麼? – Rich

+0

如果我在目錄中刪除VBS文件,當它運行,做refreshall並保存在目錄 – Jsjjharp

回答

1

試試這個

Set fs = CreateObject("Scripting.FileSystemObject") 
Set rootFolder = fs.GetFolder(fs.GetParentFolderName(wscript.ScriptFullName)) 
Set oExcel = CreateObject("Excel.Application") 

oExcel.Visible = True 
oExcel.DisplayAlerts = False 
oExcel.AskToUpdateLinks = False 
oExcel.AlertBeforeOverwriting = False 

For Each file in rootFolder.Files 
    If inStr(file.type, "Excel") > 0 Then 
    Set oWorkbook = oExcel.Workbooks.Open(file.path) 
    oWorkbook.RefreshAll 
    oWorkbook.Save 
    oWorkbook.Close 
    Set oWorkbook = Nothing 
    End If 
Next 

oExcel.Quit 
Set oExcel = Nothing 
+0

所有XLSX文件的命令只是測試它...完美! – Jsjjharp

0

你應該添加

oWorkbook.CalculateUntilAsyncQueriesDone 

oWorkbook.RefreshAll 

然後excel等待,直到保存文件

相關問題