none
How can I detect Save, Save As and BeforeClose events of Excel workbook in C# Desktop App (not .VSTO) RRS feed

  • Question

  • Hello, 

    The question is in the title:

    I have to detect Save, Save As and BeforeClose events of Excel workbook (which I open using Interop library) from Desktop App, which is written on C#. Please, advise me, what is the most efficient way to do so?

    Thank you in advance!

    Wednesday, June 7, 2017 5:12 PM

Answers

  • Hello,

    There is no Save/SaveAs event. You could use AppEvents_Event.WorkbookBeforeSave event

    How to handle events for Excel by using Visual Basic .NET shows how to handle events in a VB.NET project.

    Using C#, we could use

            static void Main(string[] args)
            {
                handleExcelEvent();
                Console.ReadKey();
    
            }
            static void handleExcelEvent()
            {
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
                xlApp.Visible = true;
    
    Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(XlApp_WorkbookBeforeClose);      
                xlApp.WorkbookBeforeClose += XlApp_WorkbookBeforeClose;
    
                Excel.AppEvents_WorkbookBeforeSaveEventHandler WorkbookBeforeSave = new Excel.AppEvents_WorkbookBeforeSaveEventHandler(XlApp_WorkbookBeforeSave);
                xlApp.WorkbookBeforeSave += XlApp_WorkbookBeforeSave;
    
            }
            private static void XlApp_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
            {
                Console.WriteLine("XlApp_WorkbookBeforeClose");
            }
    
            private static void XlApp_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {
                Console.WriteLine("XlApp_WorkbookBeforeSave");
            }
    

    Regards,

    Celeste

     



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by PG_Develop Friday, June 9, 2017 8:44 AM
    Friday, June 9, 2017 5:47 AM
    Moderator

All replies

  • https://social.msdn.microsoft.com/Forums/office/en-US/home?category=officedev

    Maybe you should post to the office developer forum.

    Wednesday, June 7, 2017 8:17 PM
  • Hi PG_Develop,

    This forum discusses and asks questions about the C# programming language, Since your issue is more related office dev, we'll move the thread to excel for dev forum for suitable support.

    Thanks for your understanding.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 8, 2017 2:32 AM
  • Hello, Cole Wu

    Thank you for review. I am looking forward for an answer

    Thursday, June 8, 2017 1:34 PM
  • Hello,

    There is no Save/SaveAs event. You could use AppEvents_Event.WorkbookBeforeSave event

    How to handle events for Excel by using Visual Basic .NET shows how to handle events in a VB.NET project.

    Using C#, we could use

            static void Main(string[] args)
            {
                handleExcelEvent();
                Console.ReadKey();
    
            }
            static void handleExcelEvent()
            {
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
                xlApp.Visible = true;
    
    Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(XlApp_WorkbookBeforeClose);      
                xlApp.WorkbookBeforeClose += XlApp_WorkbookBeforeClose;
    
                Excel.AppEvents_WorkbookBeforeSaveEventHandler WorkbookBeforeSave = new Excel.AppEvents_WorkbookBeforeSaveEventHandler(XlApp_WorkbookBeforeSave);
                xlApp.WorkbookBeforeSave += XlApp_WorkbookBeforeSave;
    
            }
            private static void XlApp_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
            {
                Console.WriteLine("XlApp_WorkbookBeforeClose");
            }
    
            private static void XlApp_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {
                Console.WriteLine("XlApp_WorkbookBeforeSave");
            }
    

    Regards,

    Celeste

     



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by PG_Develop Friday, June 9, 2017 8:44 AM
    Friday, June 9, 2017 5:47 AM
    Moderator