0

So basically my app triggers an excel macro, from a file, that updates the file and then closes it.

When I open the file I set the "DisplayAlerts = false" variable in order to ignore all popups and it works as expected in my computer... however, a colleague of mine tried to use it and for every file, he gets the popup asking if he wants to save all changes...

Checked other questions about the popups in excel but all suggested solutions use "oBook.Saved = true;" or "oBook.Close(false);", but these did not work for me.

my code is as follows:

    using Microsoft.Office.Interop.Excel;

    public static bool Trigger_Macro_From_File(string path)
    {
        ApplicationClass oExcel = null;
        Workbook oBook = null;
        try
        {
            string filename = Path.GetFileName(path);
            string macro_name = "!some_macro";
            string macro = @"'" + filename + @"'" + macro_name;

            // Create an instance of Microsoft Excel
            oExcel = new ApplicationClass
            {
                DisplayAlerts = false,
                Visible = false
            };

            oBook = oExcel.Workbooks.Open(path);

            RunMacro(oExcel, new Object[] { macro });

            oBook.Save();
            oBook.Saved = true;
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            oBook?.Close(false);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
            oBook = null;
            oExcel?.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
            oExcel = null;
            GC.Collect();
        }
    }

Does anyone know anything about this?

Thanks in advance.

Razneesh
  • 1,147
  • 3
  • 13
  • 29
Renato Martins
  • 258
  • 2
  • 7
  • Does this answer your question? [Save Overwrite and changes using Excel.Interop C#](https://stackoverflow.com/questions/49430038/save-overwrite-and-changes-using-excel-interop-c-sharp) – Arthur Attout Mar 03 '20 at 14:38
  • @ArthurAttout nope, already tried that. – Renato Martins Mar 03 '20 at 14:42
  • Possible issues: what does the macro do? Does it mess itselft with `DisplayWarnings` in some execution path it might be taking in your colleague's computer and not in yours? Is `oBook.Save();` actually executed or is the macro taking some error path that triggers the `catch` clause and skips the save logic? – InBetween Mar 03 '20 at 15:01
  • @InBetween the macro updates the values of one page using the values from the other pages. Even if that were the case and it skipped the save logic, in the "finally" case the "oBook?.Close(false);" should close the file without saving and without the popup right? – Renato Martins Mar 03 '20 at 17:11

1 Answers1

0

You could double-check that no other "Microsoft Excel" process is running in the Task Manager.

Let's say at some point in your development process you started your program and open the workbook with something like

xlWorkbook = xlApp.Workbooks.Open(filePath);

Then you encountered an exception for some reason, and killed the program without closing the file properly (workbook.Close(..), app.Quit(..) and so on).

The Microsoft Excel process is still running in the background, and has a handle on the file you want to edit. So you cannot execute an instruction that saves the file under the same name. This is why the popup is appearing.

This scenario is taken from the point of view of the developer, but the same behavior could have happened on your coworker's computer if your app crashed without quitting properly, and gets re-started.

Also, be careful that finally statement might not always be executed, so double-check which scenario could cause your app to close without releasing the COM object.

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49