我的VBS脚本正在寻找错误的目录中的文件

项目结构

我写的macros工作正常,直到我碰巧改变文件path到相对的。

这是实际的macros:

Public Sub refreshXLS() Dim fso As Object Dim folder As Object Dim file As Object Path = ThisWorkbook.Path & "\requiredSource\TestData1.xlsm" Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(Path) With Application .DisplayAlerts = False .ScreenUpdating = False .EnableEvents = False .AskToUpdateLinks = False End With For Each file In folder.Files If Right(file.Name, 4) = "xlsx" Or Right(file.Name, 3) = "xls" Then Workbooks.Open Path & file.Name ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources ActiveWorkbook.Close True End If Next With Application .DisplayAlerts = False .ScreenUpdating = True .EnableEvents = True .AskToUpdateLinks = False End With End Sub 

我更新到:

 Public Sub refreshXLS() Dim xlApp Dim xlBook Dim fso As Object Dim folder As Object Dim file As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim fullpath fullpath = fso.GetAbsolutePathName(".") Set fso = Nothing Set xlApp = CreateObject("Excel.Application") Path = xlApp.Workbooks.Open(fullpath & "\TestData1.xlsm") Set folder = fso.GetFolder(Path) With Application .DisplayAlerts = False .ScreenUpdating = False .EnableEvents = False .AskToUpdateLinks = False End With For Each file In folder.Files If Right(file.Name, 4) = "xlsx" Or Right(file.Name, 3) = "xls" Then Workbooks.Open Path & file.Name ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources ActiveWorkbook.Close True End If Next With Application .DisplayAlerts = False .ScreenUpdating = True .EnableEvents = True .AskToUpdateLinks = False End With End Sub 

我也试着用:

 Workbooks.Open FileName:= ThisWorkbook.Path & "\TestData1.xlms". 

这也没有帮助。

 Public Sub refreshXLS() Dim fso As Object Dim file As Object Dim extension As String Set fso = CreateObject("Scripting.FileSystemObject") With Application .DisplayAlerts = False .ScreenUpdating = False .EnableEvents = False .AskToUpdateLinks = False End With For Each file In fso.GetFolder(ThisWorkbook.Path).Files extension = LCase(fso.GetExtensionName(file.Path)) If extension = "xlsx" Or extension = "xls" Then With Workbooks.Open(file.Path) .UpdateLink Name:=.LinkSources .Close True End With End If Next With Application .DisplayAlerts = False .ScreenUpdating = True .EnableEvents = True .AskToUpdateLinks = False End With End Sub 

这将处理当前xlsm文件存储在同一文件夹中的所有文件。 如果要处理的文件在requiredSource文件夹下,那么你应该把GetFolder改成类似的东西

 For Each file In fso.GetFolder(fso.BuildPath(ThisWorkbook.Path, "RequiredSource")).Files 

编辑以适应评论

vbs文件可能类似于(假设之前的refreshXLS在模块内部)

 Option Explicit Const macroWorkbook = "TestData1.xlsm" Const macroName = "refreshXLS" Dim fso Set fso = WScript.CreateObject("Scripting.FileSystemObject") Dim workbook With CreateObject("Excel.Application") Set workbook = .Workbooks.Open(fso.BuildPath( _ fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _ , macroWorkbook _ )) .Application.Run "'" & Replace(workbook.Name, "'", "''") & "'!" & macroName .ActiveWorkbook.Close .Quit End With WScript.Quit 

您的代码无法正常工作,因为您更改了variablesPath

你把Path = xlApp.Workbooks.Open(fullpath & "\TestData1.xlsm")这意味着path现在是一个工作簿对象,而不是一个string了。

我build议你开始使用Option Explicit作为一种习惯。 在VBE中,选项 – >打勾需要variables声明