I have a script I run that opens an xls file (regardless of file name) in a folder named in the code and then saves that file with a new name. It has been working fine until yesterday. I now get a VBScript runtime error that says
object required: 'wb'
Any ideas on what's gone wrong would be much appreciated.
'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
You open and save files only if they have the extension .xls
, but try to close the object wb
for every file, regardless of whether you opened it first or not. Put the wb.Close True
inside the If
statement:
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
Also, you should save and close objWB2
after the loop terminates, or you might run into the same problem with that variable as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.