none
open context menu inside Outlook email message pane and get sender, subject, date, and all text or selected text RRS feed

  • Question

  • I have not done any programming using Office interop (using Outlook = Microsoft.Office.Interop.Outlook).

    Users would like  me to answer to this question: Is it possible using Visual Studio tools and the interop librares to write a program would :

    1) Add an item to the right-click context menu, which, when chosen, would perform these actions:

    2) get a handle to the current message

    3) fetch sender, subject, date

    4) fetch either the entire message text (or only the highlighted section of text if applicable)

    5) write the information out to a database

    I'm good with #5, but 1-4 I don't know the answer to.

    Thanks




    Thursday, February 4, 2016 5:45 PM

Answers

All replies

  • Hello,

    You need to develop a COM add-in for Outlook. You may consider developing a VSTO based one. VSTO is included in the paid editions of Visual Studio (+Community). See Walkthrough: Creating Your First VSTO Add-In for Outlook for more information.

    To customize context menus in Outlook 2010 and later you need to use the Fluent UI. The Customizing Context Menus in Office 2010 article describes all possible features in depth. Also you may find the Extending the User Interface in Outlook 2010 article helpful.

    VSTO provides two main ways for customizing the Fluent UI;

    Walkthrough: Creating a Custom Tab by Using the Ribbon Designer

    Walkthrough: Creating a Custom Tab by Using Ribbon XML

    > 2) get a handle to the current message

    An instance of the IRibbonControl interface is passed as a parameter to the event handler of your control. You can use the Context property which returns an object that represents the window in which the custom ribbon is about to be displayed, or the related object to which the menu customization is applied and is about to be displayed.

    > 3) fetch sender, subject, date

    See MailItem members .

    > 4) fetch either the entire message text (or only the highlighted section of text if applicable)

    The Outlook object model provides three main ways for working with item bodies:

    1. Body - a string representing the clear-text body of the Outlook item.
    2. HTMLBody - a string representing the HTML body of the specified item.
    3. Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.

    You can read more about all these ways in the Chapter 17: Working with Item Bodies. It us up to you which way is to choose to deal with the message body.

    Thursday, February 4, 2016 6:34 PM
  • Thank you for those references.   I am working with Outlook 2016 at the moment and doing the walkthrough.  Using C# and a third-party SMTP library, I sent myself a MIME email with an HTML table. When I attempt to examine HTMLBody in the debugger, I get the error CS1061 shown below, even though Intellisense offers HTMLBody as an option, and even though View Source shows the HTML markup.  I am totally new to Outlook. Do I need to enable HTML emails somewhere in Settings before this will work? Or is it that HTMLBody is no longer supported in Outlook 2016? Or do I need a using directive? Here are my using directives:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    using System.Resources;
    using System.Reflection;

    ?mailItem.HTMLBody
    error CS1061: 'MailItem' does not contain a definition for 'HTMLBody' and no extension method 'HTMLBody' accepting a first argument of type 'MailItem' could be found (are you missing a using directive or an assembly reference?)

                private void CurrentExplorer_Event()
             

              {
                Outlook.MAPIFolder selectedFolder =
                    this.Application.ActiveExplorer().CurrentFolder;
                String expMessage = "Your current folder is "
                    + selectedFolder.Name + ".\n";
                String itemMessage = "Item is unknown.";
                try
                {
                    if (this.Application.ActiveExplorer().Selection.Count > 0)
                    {
                        Object selObject = this.Application.ActiveExplorer().Selection[1];
                        if (selObject is Outlook.MailItem)
                        {


                             Outlook.MailItem mailItem =  (selObject as Outlook.MailItem);

    // BREAKPOINT HERE, examining variable mailItem

                            itemMessage = "The item is an e-mail message." +
                                " The subject is " + mailItem.Subject + ".";
                            mailItem.Display(false);
                        }





    Friday, February 12, 2016 10:08 AM
  • Hi lloydmalvern,

    First, there is the HTMLBody property of MailItem object, please obtain the value directly in the code (not in the immediately window when debug)

    Secondly, this issue is related to HTMLBody, which is not directly related to the original issue, I’d suggest asking separate questions in a new forum threads, thus, forum readers may recognize question and answers easily.

    Regards

    Starain


    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.

    Monday, February 15, 2016 1:57 AM
    Moderator