21

Is there a way to save changes to an excel spreadsheet through the excel interop (in this case I am adding a worksheet to it) without having it prompt the user if they want to overwrite the existing file with the changes. I do not want the user to even see the spreadsheet open in my application so having a message box popping up asking them if they want to overwrite the file seems very out of place and possibly confusing to the user.

I am using the workbook.SaveAs(fileloaction) method.

Here is where I am initializing the COM reference objects for the excel interop.

private Excel.Application app = null;
    private Excel.Workbook workbook = null;

    public Excel.Workbook Workbook
    {
        get { return workbook; }
        set { workbook = value; }
    }
    private Excel.Worksheet worksheet = null;
    private Excel.Range workSheet_range = null;

Below is the code I am using to close/save the excel file. The workbook.close() method line is the one that is reportedly throwing the unhandled exception.

 workbook.Close(true, startForm.excelFileLocation, Missing.Value);
            Marshal.ReleaseComObject(app);
            app = null;
            System.GC.Collect();

3 Answers 3

57

Basically, all you need is ExcelApp.DisplayAlerts = False - Here's how I do it, though:

ExcelApp.DisplayAlerts = False
ExcelWorkbook.Close(SaveChanges:=True, Filename:=CurDir & FileToSave)

Hope this helps

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome thanks it worked! If only now I could find a way to close it like you are doing above without it causing my c# applicaiton to throw an unhandled exception and crash. I can't close the application after saving and closing the excel file either. It needs to stay open.
Can you post a bit of code (or post a new question with that code if it doesn't suit this question)... That seems strange and, I'm sure, easily fixable...
I have updated the post with some code. If you need more let me know. I have already posted multiple threads about this problem, tried several solution, but have yet to be able to close/save the excel workbook without throwing an unhandled exception and crashing the c# application.
Also, the unhandled exception says 'The object invoked has disconnected from its clients.'
I would definitely need more code... the first thing I wonder is if it has to do with the startForm.excelFileLocation - Did you try changing it with a string of just a file location? - I'm wondering if it has to do with a threading issue... Either way, more code would deifnitely help us along...
8

Only this code will Require for stop override alert or Template already in use

ExcelApp.DisplayAlerts = False

Comments

1

I know this is an old post, but I wanted to share a way to make this work without causing possible frustration in the future.

First what I do not like about using: ExcelApp.DisplayAlerts = False

Setting this flag will set this property on the excel file, not just in your program. This means that if a user makes changes to the file and closes it (by clicking the X), they will not be prompted to save the file and will cause frustration later. It will also disable any other prompts excel would typically post.

I like checking if the file exists before saving it:

        if (File.Exists(SaveAsName))
        {
            File.Delete(SaveAsName); 
        }

1 Comment

No man, I tryed here make a change and closed without saving and Excel prompted to save the file, in your way will work as well but isn't as simples as the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.