0

For some event, the subscription by hooking the event directly object is failing but working when you use a local variable. I haven't figured out why yet, who does?

I came across this mystery as I start working with CommandBarsEvents.OnUpdate, in order to detect interaction with a shape, what can comes with certain problems as described here. There are other events with that problem?

Community
  • 1
  • 1
Stefan
  • 372
  • 1
  • 16

1 Answers1

0

to keep clear here comes an (incomplete) overview of events which are affected and which not

  • events what DO need a local variable

    1. this.Application.CommandBars.OnUpdate
      .

    Their code has to be like this:

    using Office = Microsoft.Office.Core;
    namespace Project
    {
        public partial class AddIn
        {      
            private Office.CommandBars commandBars; //declaration of local variable
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                commandBars = this.Application.CommandBars; //initialization of local variable
                commandBars.OnUpdate += new Office._CommandBarsEvents_OnUpdateEventHandler(commandBars_OnUpdate); //"indirect" subscription to event`
            }
    
        }
    }
    
  • events what NOT need a local variable

    1. ((InteropExcel.AppEvents_Event)Application).NewWorkbook
    2. this.Application.WorkbookNewSheet
    3. this.Application.WorkbookOpen
      .

    Their code can be like this:

    using Office = Microsoft.Office.Core;
    namespace Project
    {
        public partial class AddIn
        {      
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                 ((InteropExcel.AppEvents_Event)Application).NewWorkbook += new InteropExcel.AppEvents_NewWorkbookEventHandler(Excel_NewWorkbook_EventHandler); //"direct" subscription to event`
            }
    
        }
    }   
    
Stefan
  • 372
  • 1
  • 16