用不同的文件名保存打开的工作簿(另存为)
Save open workbook with a different file name (SaveAs)
我有一个脚本 运行,它在代码中指定的文件夹中打开一个 xls 文件(无论文件名如何),然后用新名称保存该文件。直到昨天,它一直运行良好。我现在收到一个 VBScript 运行time 错误,上面写着
object required: '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\Balloon15"
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
的文件才能打开和保存,但无论您是否先打开它,请尝试关闭每个文件的对象 wb
。把 wb.Close True
放在 里面 If
语句:
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
此外,您应该在 循环终止后保存并关闭objWB2
,否则您也可能运行 遇到与该变量相同的问题。
我有一个脚本 运行,它在代码中指定的文件夹中打开一个 xls 文件(无论文件名如何),然后用新名称保存该文件。直到昨天,它一直运行良好。我现在收到一个 VBScript 运行time 错误,上面写着
object required: '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\Balloon15"
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
的文件才能打开和保存,但无论您是否先打开它,请尝试关闭每个文件的对象 wb
。把 wb.Close True
放在 里面 If
语句:
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
此外,您应该在 循环终止后保存并关闭objWB2
,否则您也可能运行 遇到与该变量相同的问题。