Excel Automation - how to just say No

11,996

Solution 1

You need to supply a False parameter on the Close method

xlWorkBook.Close(False)

From Excel VBA Help:

Close method as it applies to the Workbook object.

Closes the object.

expression.Close(SaveChanges, Filename, RouteWorkbook)

expression Required. An expression that returns one of the above objects.

SaveChanges Optional Variant. If there are no changes to the workbook, this argument is ignored. If there are changes to the workbook and the workbook appears in other open windows, this argument is ignored. If there are changes to the workbook but the workbook doesn't appear in any other open windows, this argument specifies whether changes should be saved, as shown in the following table.

Value Action True Saves the changes to the workbook. If there is not yet a file name associated with the workbook, then FileName is used. If FileName is omitted, the user is asked to supply a file name. False Does not save the changes to this file. Omitted Displays a dialog box asking the user whether or not to save changes.

FileName Optional Variant. Save changes under this file name.

RouteWorkbook Optional Variant. If the workbook doesn't need to be routed to the next recipient (if it has no routing slip or has already been routed), this argument is ignored. Otherwise, Microsoft Excel routes the workbook as shown in the following table.

Value Meaning True Sends the workbook to the next recipient. False Doesn't send the workbook. Omitted Displays a dialog box asking the user whether the workbook should be sent.

Solution 2

Per this documentation: http://msdn.microsoft.com/en-us/library/bb223560.aspx

If you set the Saved property for a workbook to True without saving the workbook to the disk, Microsoft Excel will quit without asking you to save the workbook.

Solution 3

It is good to use Close() to avoid this error message but what if you are using the version like v11.0 which have close() that takes no parameter so the problem still does not solve for this case. To deal with this issue you may use Marshal.RealeaseCOMObject BUT remember there are some issues with that.

In response to your "how to just say No", you can use:

xlApp.DisplayAlerts= false;  // use just above the Close() method calls
xlApp.Workbooks.Close(); // here no arguments can be used.

Hope it helps!

Share:
11,996
Chris Burgess
Author by

Chris Burgess

Updated on June 19, 2022

Comments

  • Chris Burgess
    Chris Burgess almost 2 years

    I'm working on a vb.net console app that opens a number of spreadsheets one by one, reads in a couple cells and closes the file.

    Some of the spreadsheets have formulas that Excel recalculates when I open the file, and I'm getting a dialog box asking me if I want to save changes to the spreadsheet when I close it.

    Message: "Do you want to save the changes to myfile.xls? Microsoft Office Excel recalculates formulas when opening files last saved by an earlier version of Excel"

    How do I automatically decline that dialog box?

                Dim newCBD As New CBD
                Dim xlApp As Excel.Application
                Dim xlWorkBook As Excel.Workbook
                Dim xlWorkSheet As Excel.Worksheet
    
                xlApp = New Excel.ApplicationClass
                xlWorkBook = xlApp.Workbooks.Open(myFile)
                xlWorkSheet = xlWorkBook.Worksheets(1)
    
                ...do a bunch of junk here
    
                xlWorkBook.Close()
                xlApp.Quit()
    

    Thanks!