views:

844

answers:

2

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!

+6  A: 

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.

DJ
+1  A: 

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.

tehblanx