none
I want to Fire an event when user clicks the attach file button. But the problem i m facing in my code is it is not fired first time it is fired second time when we compose mail. RRS feed

  • Question

  •  private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        selectExplorers = this.Application.Explorers;
        selectExplorers.NewExplorer +=new Outlook .ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
        AddToolbar();


        //Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;


        CreateNewPST_Click();

        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=  new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
        Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);


        foreach (Outlook.Inspector inspector in inspectors)
        {
            Inspectors_NewInspector(inspector);
        }

    }

    And My newinspector Code is

              

      void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {

                Inspector insp = (Inspector)Inspector;
                MailItem item = (MailItem)insp.CurrentItem;
                myAttachHandler = new Outlook.ItemEvents_10_AttachmentAddEventHandler(gp2_fnAttachFile);
                myDetachHandler = new Outlook.ItemEvents_10_AttachmentRemoveEventHandler(gp2_fnDetachFile);
                item.AttachmentAdd += myAttachHandler;
                item.AttachmentRemove += myDetachHandler;

                inspectorWrappersValue.Add(Inspector, new InspectorWrapper(Inspector));

                   }    }

    void gp2_fnAttachFile(Attachment myAttach)
        {
            MessageBox.Show("Added");
            attachcount++;

        }
        void gp2_fnDetachFile(Outlook.Attachment myAttach)
        {
            attachcount--;
        }

    This  gp2_fnAttachFile event is not fired when we open mailItem window for first time. It is fired if we open it the second time.

    Please guide me if anything is missing in my code above 

          
    Sunday, October 23, 2016 7:25 AM

Answers

All replies

  •  private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        selectExplorers = this.Application.Explorers;
        selectExplorers.NewExplorer +=new Outlook .ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
        AddToolbar();


        //Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;


        CreateNewPST_Click();

        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=  new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
        Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);


        foreach (Outlook.Inspector inspector in inspectors)
        {
            Inspectors_NewInspector(inspector);
        }

    }

    And My newinspector Code is

              

      void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            if (Inspector.CurrentItem is Outlook.MailItem)
            {

                Inspector insp = (Inspector)Inspector;
                MailItem item = (MailItem)insp.CurrentItem;
                myAttachHandler = new Outlook.ItemEvents_10_AttachmentAddEventHandler(gp2_fnAttachFile);
                myDetachHandler = new Outlook.ItemEvents_10_AttachmentRemoveEventHandler(gp2_fnDetachFile);
                item.AttachmentAdd += myAttachHandler;
                item.AttachmentRemove += myDetachHandler;

                inspectorWrappersValue.Add(Inspector, new InspectorWrapper(Inspector));

                   }    }

    void gp2_fnAttachFile(Attachment myAttach)
        {
            MessageBox.Show("Added");
            attachcount++;

        }
        void gp2_fnDetachFile(Outlook.Attachment myAttach)
        {
            attachcount--;
        }

    This  gp2_fnAttachFile event is not fired when we open mailItem window for first time. It is fired if we open it the second time.

    Please guide me if anything is missing in my code above 

          
    Sunday, October 23, 2016 7:23 AM
  • Hello,

    I'd suggest accessing the CurrentItem property of the Inspector class in the first Activate event of the Inspector class. The NewInspector event handler is not a right place for such calls. Sometimes you may get a fake item or not initialized completely. 

    You may find the Developing an Inspector Wrapper for Outlook 2010 article helpful. 


    [custom.development]

    Sunday, October 23, 2016 8:01 PM
  • Hi SCoder113,

    it looks like you had created a 2 threads with the same issue.

    so for avoid duplication of threads I merge these 2 threads.

    thanks for your understanding.

    Regards

    Deepak


    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.

    Monday, October 24, 2016 3:00 AM
    Moderator