none
VSTO Outlook 2010 Custom Button to change the subject of a new email RRS feed

  • Question

  • I am new to VSTO. I wrote an Add-in example for Outlook 2010 and it works fine.<o:p></o:p>

    Later on I added a custom group to the built-in new email message tab ribbon and added a button using the default name Button1. When I run the add-in the button shows up fine.<o:p></o:p>

    I want to click on this button and change the subject and the body content. I tried to add a private sub Button1_Click with code that changes the subject to “subject example” and the body to “body example” and could not make it work. Can you show me the code that I need to add to make my button1 work. Thanks for your help

    Wednesday, September 23, 2015 6:56 PM

Answers

  • Fei,

    There is no need to use the ActiveInspector method of the Application class:

    private void button3_Click(object sender, RibbonControlEventArgs e)
    {
         Outlook.Inspector inspector = e.Context as Outlook.Inspector;  
         if (inspector != null)
         {
             MailItem mi = inspector.CurrentItem;
             if (mi != null)
             {
                mi.Subject = "Hello Word!";
             }
         }
    }

    Thursday, September 24, 2015 8:59 AM

All replies

  • Hello,

    VSTO provides two main ways for customizing the Ribbon UI:

    Essentially you need to add the onAction attribute for the button and then add the corresponding callback to the code.

    Read more about the Fluent UI (aka Ribbon UI) in the following series of articles in MSDN:

    Wednesday, September 23, 2015 7:48 PM
  • Can you show me the code that I need to add to make my button1 work. Thanks.
    Wednesday, September 23, 2015 7:56 PM
  • To get the currently shown in the inspector window Outlook item you can use the ActiveInspector method of the Application class or use the IRibbonControl.Context property which you may cast to the Inspector class. Then you can use the `CurrentItem` property to get the Outlook item. For example, a raw sketch:

        Dim insp as Inspector = control.Context
        Dim mailItem as MailItem = insp.CurrentItem
        If Not (mailItem Is Nothing) Then
            If mailItem.EntryID Is Nothing Then
                mailItem.Subject = "This text was added by using code"
                mailItem.Body = "This text was added by using code"
            End If
        End If

    Wednesday, September 23, 2015 10:10 PM
  • Thanks for not giving up on me. I tried the code you suggested and got the following errors:

    Type 'Inspector' is not defined.

    'control' is not declared. It may be inaccessible due to its protection level.

    Type 'MailItem' is not defined.

    This is why I asked you to provided the complete code. I am new to VSTO office solutions and could not find a way to make it work. Thanks.

    Wednesday, September 23, 2015 10:45 PM
  • Hi Meni21,

    Based on my understanding, we need to specific the Ribbon Type to Microsoft.Outlook.Mail.Compose. And please ensure that there is an inspect window was opened for the new mail item. Then we can use code below if you customize the ribbon via Ribbon Designer:

      private void button3_Click(object sender, RibbonControlEventArgs e)
            {
                if (Globals.ThisAddIn.Application.ActiveInspector() != null)
                {
                    MailItem mi = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                    if (mi != null)
                    {
                        mi.Subject = "Hello Word!";
                    }
                }
            }
    Regards & Fei


    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.

    Thursday, September 24, 2015 8:45 AM
    Moderator
  • You need to add the using statement with the Outlook namespace or prepend the objects declared with the fully-qualified name, for example:

    Microsoft.Office.Interop.Outlook

    or

    using Microsoft.Office.Interop.Outlook;

    Thursday, September 24, 2015 8:54 AM
  • Fei,

    There is no need to use the ActiveInspector method of the Application class:

    private void button3_Click(object sender, RibbonControlEventArgs e)
    {
         Outlook.Inspector inspector = e.Context as Outlook.Inspector;  
         if (inspector != null)
         {
             MailItem mi = inspector.CurrentItem;
             if (mi != null)
             {
                mi.Subject = "Hello Word!";
             }
         }
    }

    Thursday, September 24, 2015 8:59 AM