使用不同的文件名保存打开的工作簿(SaveAs)

时间:2016-01-06 19:21:47

标签: excel vbscript save-as

我运行的脚本会在代码中指定的文件夹中打开xls文件(无论文件名如何),然后使用新名称保存该文件。它一直工作到昨天。我现在得到一个说

的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") 

1 个答案:

答案 0 :(得分:1)

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 ,否则您也可能遇到与该变量相同的问题。