Computing Staff
  • 8

VBS Script Open File, Run Macro But Does Not Save The File

  • 8

Hi,

I have the following vbs code that works great, but it doesn’t save the file after running the macro? Does anyone have any ideas how to fix this?

 

Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("G:\Operations Excellence\Forms\ASA Forms\Start-Stop\Start-Stop by Plant-rev1.xlsm", 0, True) 
  xlApp.Run "RDB_Worksheet_Or_Worksheets_To_PDF_And_Create_Mail"
  xlApp.Save
  xlApp.DisplayAlerts = False
  xlApp.ActiveWorkbook.Close
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 

End Sub

Thank you,
Sandi

Share

1 Answer

  1. I’ll bet that …

    xlBook.SaveAs  _
    "G:\Operations Excellence\Forms\ASA Forms\Start-Stop\Start-Stop by Plant.xlsm"
    
    xlBook.Close 

    …made all the difference in the world!

    Ya gotta reference the correct object or things can get really weird.

    Thanks for posting the final version for the archives. It may just help someone else in the future.

    Click Here Before Posting Data or VBA Code —> How To Post Data or Code.

    • 0