How to Close Excel file from VBScript without being prompted?
Asked Answered
M

5

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
Maharani answered 7/1, 2014 at 18:32 Comment(2)
objWorkbook.Close False and/or set objExcel.DisplyAlerts=False before closing the workbook.Goatfish
@TIm Williams. Thanks! This combination works. Please reply as an Answer and I will select it. Thanks again.Maharani
G
25

This should do it:

objExcel.DisplayAlerts = False

objWorkbook.Close False 
Goatfish answered 7/1, 2014 at 18:59 Comment(0)
R
2

Have you tried:

    objWorkbook.Close False
Rusticus answered 7/1, 2014 at 18:39 Comment(0)
E
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 
Evangelin answered 22/5, 2015 at 9:50 Comment(0)
P
0

Why not just this?

On Error Resume Next
GetObject(, "Excel.Application").Workbooks("file_name.xlsx").Close False
On Error Goto 0
Prevalent answered 9/8 at 6:8 Comment(0)
T
-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.

Tomekatomes answered 21/10, 2014 at 21:57 Comment(1)
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.Shipman

© 2022 - 2024 — McMap. All rights reserved.