调整VBScript以刷新目录中所有excel文件的外部数据

时间:2014-03-04 12:49:57

标签: excel vbscript

我有一个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文件(即相对路径)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你应该添加

oWorkbook.CalculateUntilAsyncQueriesDone

之后

oWorkbook.RefreshAll

然后excel等待所有数据更新,然后再保存文件