Search code examples
c#outlookvstooutlook-addinoffice-addins

C# - Attach file in Outlook Inline Message


I'm trying to attach a file in Outlook Inline Message Window(Reply, ReplyAll or Forward) with a new button in the ribbon.

Currently using VSTO C# to develop it

The button is working if it's a new mail window, but when I select a mail from the inbox folder it does nothing.

Checking with System.Diagnostic.Debug.WriteLine("Text 1"); I can see it's not working this part of the code:

public void addAttachment(string valueText)
        {
             Outlook._Application _app = new Outlook.Application();
             MailItem mailItem = _app.ActiveInspector().CurrentItem;

             if (mailItem != null && mailItem.Class == OlObjectClass.olMail)
             {
                 if (string.IsNullOrEmpty(mailItem.Subject) || mailItem.Subject.StartsWith("Untitled Message"))
                 {
                     System.Diagnostics.Debug.WriteLine("This is the inspector 1 " + mailItem);
                     mailItem.Attachments.Add(valueText); // --> This is working fine
                 }
                 else if (mailItem.Subject.StartsWith("RE:") || mailItem.Subject.StartsWith("FW:"))
                 {
                     System.Diagnostics.Debug.WriteLine("This is the inspector 2 " + mailItem);
                     mailItem.Attachments.Add(valueText); // --> This is what is not working
                 }
                 else
                 {
                     Console.WriteLine("Unknown window");
                 }
             }
        }

The idea is when I click the button in the ribbon it will run a custom script and after this will take a file to be attached in the new mail automatically (It can be a New Mail Full Window or just select an email and click Reply, ReplyAll or Forward).

EDIT: I tried to find it by Subject.StartsWith, but not working, it seems like there is not ActiveInspector when I use these buttons

Reply, ReplyAll and Forward Buttons


Solution

  • First of all, in your VSTO add-in there is no need to create a new Outlook Application instance:

    Outlook._Application _app = new Outlook.Application();
    

    Instead, you can use the Application property of your add-in class (see ThisAddin). Also you can access the Application instance anywhere by using the static Globals class, see Global access to objects in Office projects for more information.

    Calling the ActiveInspector method doesn't make any sense in case of inline emails:

    MailItem mailItem = _app.ActiveInspector().CurrentItem;
    

    Instead, you need to use the ActiveInlineResponse property to get the item opened inline in the Explorer window:

    MailItem mailItem = _app.ActiveExplorer().ActiveInlineResponse;
    

    The property returns an item object representing the active inline response item in the explorer reading pane.

    Also you may find the Explorer.InlineResponse event helpful which is fired when the user performs an action that causes an inline response to appear in the Reading Pane.