11

I have a VB Script that opens an Excel file and runs a macro. I am trying to close this excel file(without saving any changes) without being prompted to save. I have set the 'Saved' property to true. But I am still prompted with the Save window. I read somewhere that I have to disable the macro. Not sure how? I would like to close the excel file without saving and without prompting.

Dim objExcel, objWorkbook 

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("c:\aaa\Test.xls")
objExcel.Visible = True
objExcel.Run "Extract_PLStatements"
objworkbook.Saved = True
objWorkbook.Close

objExcel.Quit

Set objWorkbook = Nothing
Set objExcel = Nothing

WScript.Quit
2
  • 2
    objWorkbook.Close False and/or set objExcel.DisplyAlerts=False before closing the workbook. Jan 7, 2014 at 18:40
  • @TIm Williams. Thanks! This combination works. Please reply as an Answer and I will select it. Thanks again.
    – stackuser
    Jan 7, 2014 at 18:52

4 Answers 4

25

This should do it:

objExcel.DisplayAlerts = False

objWorkbook.Close False 
0
2

Have you tried:

    objWorkbook.Close False
0
0

Try this. This closes all the excel instances without asking to save. You will however loose any unsaved data.

Do While  True  
    Dim objExcel
    On Error Resume Next
    Set objExcel = GetObject(,"Excel.Application")
    If Err.Number <> 0 Then
        Exit Do
    End If
    On Error GoTo 0
    objExcel.DisplayAlerts = False
    objExcel.Quit
    Set objExcel = nothing
Loop 
-4

Try this one, much easier!

FILENAME CMDS DDE 'Excel|system'; 

DATA _NULL_; 
FILE CMDS; 
PUT '[Workbook.Activate("FILENAME.XLS")]';
PUT "[SAVE()]";
PUT "[CLOSE("'"'"C:\FILENAME.XLS"'"'")]"; 
RUN;

Sorry this is using SAS.

1
  • 7
    Why are you writing an answer using SAS when the OP is clearly asking for VBScript? This doesn't help answer the question. Besides which, there is already an accepted answer, so adding this in provides little more gain.
    – rayryeng
    Oct 21, 2014 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.