none
Is it possible to catch "Send" event on Outlook add-in in a button's onAction function? RRS feed

  • Question

  • Because I'm in the ribbon's class, there is no pointer to the Outlook.Application object. So, I cannot use 

    Application.ItemSend
     	+= new ApplicationEvents_11_ItemSendEventHandler(MyItemSendEventHandler)

    this event handler.

    How can I reach Outlook.Application object in ribbon class or are there another way to catch send event?



    • Edited by goktugbc Thursday, June 19, 2014 8:55 AM
    Thursday, June 19, 2014 7:38 AM

Answers

  •  

    public void SendEnMail(Office.IRibbonControl control) //OnAction Function
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            myMail.Display(true);
            Outlook.Application application = Globals.ThisAddIn.Application;
            application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
            System.Windows.Forms.MessageBox.Show(a);
            Cancel = true;
        }

    Hi goktugbc,

    The problem is that you create another outlook application instance in this code snippet, try to use the default application instance to create a new mail item:

    public void SendEnMail(Office.IRibbonControl control)
            {
                Outlook.Application application = Globals.ThisAddIn.Application;
                application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
    
                Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem).Display(true);
            }
    
            private void Application_ItemSend(object Item, ref bool Cancel)
            {
                string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
                System.Windows.Forms.MessageBox.Show(a);
                Cancel = true;
            }
    I've tested this code, it works fine. When the button with the attribute onAction="SendEnMail" is clicked, it'll firstly subscribe an event handler Application_ItemSend to the application's ItemSend event. Then it'll create a new mail item, and when I clicked Send, it'll show me the content I just writed.


    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.


    Friday, June 20, 2014 6:04 AM
    Moderator

All replies

  • Hello,

    In a VSTO based add-in try using the following line of code:

    Globals.ThisAddin.Application

    You may find the Walkthrough: Updating the Controls on a Ribbon at Run Time in MSDN helpful.
    Thursday, June 19, 2014 10:05 AM
  • If I understood correctly, you said I should use

    Globals.ThisAddIn.Application.ItemSend
     	+= new ApplicationEvents_11_ItemSendEventHandler(MyItemSendEventHandler)
    Am I correct?

    Thursday, June 19, 2014 10:20 AM
  • Yes, you are. You can find such code in the article mentioned in my previous message.
    Thursday, June 19, 2014 10:54 AM
  • Yes, I've seen the article. But when I write the code, it didn't work.

    public void SendEnMail(Office.IRibbonControl control) //OnAction Function
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            myMail.Display(true);
            Outlook.Application application = Globals.ThisAddIn.Application;
            application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
            System.Windows.Forms.MessageBox.Show(a);
            Cancel = true;
        }

    This code cannot catch the ItemSend event and cannot reach the mail's body.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
             Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
    void Application_ItemSend(object Item, ref bool Cancel)
        {
            string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
            System.Windows.Forms.MessageBox.Show(a);
            Cancel = true;
        }

    However, when I write ThisAddIn class like this, it is working and I can reach the mail's body.

    I want to do that in ribbon's class, but how? 


    • Edited by goktugbc Thursday, June 19, 2014 11:00 AM
    Thursday, June 19, 2014 10:59 AM
  • Did you try to check the Application properties? Do you get a valid instance using the following property?

    Globals.ThisAddIn.Application

    It seems you need to declare the application object globally for preventing it from swiping by the garbage collector. Is this the case?

    As a workaround you can also get an Application instance using the argument passed to one of your Ribbon event handlers:

    Office.IRibbonControl control

    The Context property which represents the active window containing the Ribbon user interface that triggers a callback procedure.

    Thursday, June 19, 2014 11:07 AM
  • Sorry but, I'm newbie at Outlook Add-In development and C# so, I didn't understand your last answer at all.
    Thursday, June 19, 2014 11:24 AM
  • 1. Did you try to check out any property of the Application object accessed using the following code?

    Globals.ThisAddIn.Application

    I'd recommend declaring the Application object at the class scope, not in the method.

    2. You can cast the control.Context to an Explorer or Inspector class and then get the Application instance.

    Thursday, June 19, 2014 12:12 PM
  • If I declare the Application at the class scope, it gives "NullReferenceException".
    • Edited by goktugbc Thursday, June 19, 2014 12:22 PM
    Thursday, June 19, 2014 12:20 PM
  • How did you initialize it?

    Did you try to use the Context property?

    Thursday, June 19, 2014 12:58 PM
  •  

    public void SendEnMail(Office.IRibbonControl control) //OnAction Function
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            myMail.Display(true);
            Outlook.Application application = Globals.ThisAddIn.Application;
            application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
            System.Windows.Forms.MessageBox.Show(a);
            Cancel = true;
        }

    Hi goktugbc,

    The problem is that you create another outlook application instance in this code snippet, try to use the default application instance to create a new mail item:

    public void SendEnMail(Office.IRibbonControl control)
            {
                Outlook.Application application = Globals.ThisAddIn.Application;
                application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
    
                Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem).Display(true);
            }
    
            private void Application_ItemSend(object Item, ref bool Cancel)
            {
                string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
                System.Windows.Forms.MessageBox.Show(a);
                Cancel = true;
            }
    I've tested this code, it works fine. When the button with the attribute onAction="SendEnMail" is clicked, it'll firstly subscribe an event handler Application_ItemSend to the application's ItemSend event. Then it'll create a new mail item, and when I clicked Send, it'll show me the content I just writed.


    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.


    Friday, June 20, 2014 6:04 AM
    Moderator