用另一个文件名保存打开的工作簿(另存为)

我有一个我运行的脚本,在代码中命名的文件夹中打开一个xls文件(不pipe文件名),然后用一个新名字保存该文件。 直到昨天一直工作正常。 我现在得到一个VBScript运行时错误,说

需要的对象:'wb'

什么是错误的任何想法将不胜感激。

'Opens Excel Set app = CreateObject("Excel.Application") 'sets variable to Division Market Share Report v2.0.xls strPath_Excel = "C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Division Market Share Report v2.0.xls" 'Opens Division Market Share Report v2.0.xls Set objWB2 = app.Workbooks.Open(strPath_Excel) app.Visible = False app.DisplayAlerts = False 'This section opens files with "xls" extension in the C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon directory Set fso = CreateObject("Scripting.FileSystemObject") For Each f In fso.GetFolder("C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon").Files If LCase(fso.GetExtensionName(f)) = "xls" Then Set wb = app.Workbooks.Open(f.Path) 'This saves the current month BOBJ report with a new name ("...Previous Month") in order to agree with the file name coded into the macro that runs next 'This has to be done here because there is a specific hard-coded reference to the file name in the subsequently run macro wb.SaveAs "C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon\Division Balloon Market Share Report Previous Month.xls" End If 'This runs the CopyPasteMonthlyData macro saved to the Division Market Share Report v2.0.xls workbook app.Run "'Division Market Share Report v2.0.xls'!CopyPasteMonthlyDataBalloon" 'This saves the updated Balloon Market Share Report Previous Month file and closes it objWB2.Save objWB2.Close True wb.Close True Next 'This section moves all files in the C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon path to the 2015 folder in the same path Set fldr = fso.GetFolder("C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon") Set Collec_Files = fldr.Files For Each File in Collec_Files fso.MoveFile "C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon\*.xlsx", "C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon\2015" fso.DeleteFile "C:\Users\DSO7761\Desktop\Ideas\Market Share Report Automation\Market Share Reports\Balloon\*" Next app.Quit Set app = Nothing Set objWB2 = Nothing Set wb = Nothing Set fso = Nothing 'Message Box with just prompt message x=MsgBox("The BAlloon Marketshare Report update is complete.",vbInformation,"Script Run Successfully") 

 For Each f In fso.GetFolder("C:\Use...oon").Files If LCase(fso.GetExtensionName(f)) = "xls" Then Set wb = app.Workbooks.Open(f.Path) ... wb.SaveAs "C:\Use...nth.xls" End If ... wb.Close True Next 

只有具有扩展名.xls文件,才能打开和保存文件,但不pipe您是先打开还是不打开,尝试closures每个文件的对象wb 。 将If语句中的wb.Close True 放入

 For Each f In fso.GetFolder("C:\Use...oon").Files If LCase(fso.GetExtensionName(f)) = "xls" Then Set wb = app.Workbooks.Open(f.Path) ... wb.SaveAs "C:\Use...nth.xls" ... wb.Close True End If Next 

另外,你应该在循环结束保存并closuresobjWB2 ,否则你也可能遇到同样的问题。