Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Automation - how to just say No

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!

like image 693
Chris Burgess Avatar asked Apr 15 '09 20:04

Chris Burgess


People also ask

How do I stop Excel from prompting?

In Excel, select Edit Links in the Queries & Connections group on the Data tab. Click Startup Prompt. Click the Don't display the alert and don't update automatic links option.

How do you automate actions in Excel?

To automate a repetitive task, you can record a macro with the Macro Recorder in Microsoft Excel. Imagine you have dates in random formats and you want to apply a single format to all of them. A macro can do that for you. You can record a macro applying the format you want, and then replay the macro whenever needed.


1 Answers

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.

like image 153
DJ. Avatar answered Sep 25 '22 15:09

DJ.