views:

139

answers:

3

In a line of business application I'm working on, we have the ability to export to Excel, which we achieve through Excel automation. We have two methods of exporting - one which saves the files and then goes to attach them to an Outlook email (again through automation); the other is effectively a 'preview' which performs the export to Excel, but makes the application visible at the end, rather than saving/emailing.

One of the issues being reported is that when they preview, make no changes, and try to close Excel, it prompts with the standard "do you want to save your changes". Their opinion is that as they haven't made any changes, it shouldn't show this message.

Is there any way of suppressing this message, short of forcing a save to a location that we have to manually clean out "at a later point in time"?

A: 

How about objWorkbook.Close false

this parameter "false" is responsible for SaveChanges

Does this solves the problem?

Or if you want to force workbook to save at any time put "true" instead.

Cornelius
We don't want to call `Close()` though, as that would through away the workbook, before they've had a chance to preview the output?
Rowland Shaw
A: 

Hi Rowland,

The following way worked for me:

Set XLHandle =  CreateObject("Excel.Application")
XLHandle.DisplayAlerts = False
XLHandle.Visible = True

Set XLBook = XLHandle.WorkBooks.Open("c:\temp\1.xls")

'This is where your user reviews the book and manually closes it.
'If you want to test out this code set a breakpoint at the next line, manually close Excel workbook then proceed with the execution.
'In your App you would need to implement certain synchronization with user actions instead of setting a breakpoint.   

XLHandle.Quit

Set XLBook = Nothing
Set XLHandle = Nothing 

This way you suppress all the pop-ups. You can access DisplayAlerts property any time, not only after creation of automation object.

Thank you, Albert Gareev

http://automation-beyond.com/

Albert Gareev
This will also close the workbook, something we do not want to happen. We need the Excel application to remain open until closed by the end user, and then not to prompt to save changes.
Rowland Shaw
@Rowland. No problem. I've just updated the code example for you.
Albert Gareev
A: 

Setting the Workbook::Saved property property to true will stop Excel from prompting to save changes (as long as you don't make any subsequent edits)

Rowland Shaw