none
Add events for a workbook by WorkbookOpen event RRS feed

  • Question

  • When a workbook opened, I want to add event handler(like SelectionChange,Change) for it, so I use event WorkbookOpen:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
            }
    
    private void Application_WorkbookOpen(Excel.Workbook Wb)
            {
                Excel.Worksheet sheet = this.Application.Worksheets[1];
                sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(ChangeHandler);
                System.Windows.Forms.MessageBox.Show(":(");
            }
    
    private void ChangeHandler(Excel.Range Target)
            {
                System.Windows.Forms.MessageBox.Show(":((");
            }

    But When debug or load the add-in into Excel application, only the first workbook works fine. Once I open the second workbook, Both SelectionChange handler doesn't work.

    All code seems to correctly run in debug(no error or exception) except no effect.

    ps: Adding event handler for a new opened workbook by clicking a button works fine. It seems that the problem is about WorkbookOpen event.

    Monday, June 6, 2016 3:23 AM

Answers

All replies

  • Hi dnzhi,

    when I try to run your code on my side I find that ThisAddIn_Startup event fires but Application_WorkbookOpen and ChangeHandler did not fire a single time.

    when I open a new workbook at that time also it did not get fired.

    it looks like in your case it only called 1 time but In my case it did not called.

    I am using Excel 2016 which version of Excel you are using?

    did you try to called your events on buttons click on ribbon. you should try to check it whether it work or not.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 7, 2016 3:07 AM
    Moderator
  • I am using Excel 2010.

    As I say in 'ps', I tryed to add ChangeHandler on buttons click. It works fine.

    Tuesday, June 7, 2016 4:03 AM
  • Hi dnzhi,

    did you try to calling the workbook_open event from Internal startup.

    this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);

    are you trying to open another workbook from local location?

    sometimes it was found that when file on network does not fire this event.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 7, 2016 7:11 AM
    Moderator
  • it still doesn't work.

    private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
                this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
    
            }

    Workbooks are all in local location. 

    Monday, June 13, 2016 5:54 AM
  • Hi dnzhi,

    you can also try to use code like below.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
     {
                ((Excel.ApplicationEvents4_Event)Application).NewWorkBook += ThisAddIn_NewWorkBook;
                Application.WorkBookOpen += ThisAddIn_WorkBookOpen;
     }
    

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 14, 2016 5:15 AM
    Moderator
  • The NewWorkBook only get fired when I create new workbook. So there are almost no different.

    The point is that I want to handler workbook event for every workbook opened.

    Tuesday, June 14, 2016 8:22 AM
  • Hi dnzhi,

    I also try to call workbook open event but I also face same problem like you.

    it call the event when run from visual studio but it not get called when try to open workbook from outside.

    I will try to find solution as I get something helpful I will provide you.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 15, 2016 7:01 AM
    Moderator
  • Hi dnzhi,

    I find the another thread which have similar problem like you and it is answered now.

    so please visit the link below.

    Hope it also solves your issue.

    Excel - dynamically added event get lost when opening new workbook

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 21, 2016 5:33 AM
    Moderator