Search code examples
c#outlookvstooutlook-addin

How can i get mailItem when replying to an email?


I wrote an outlook vsto addin that converts files added via the button on the ribbon into links. First, I click on the create link button on the ribbon and select the files to be uploaded. Then I upload the files and add them to the body of the mail. There is no problem when i create a new mail.

But when I try to create a file link and attach it to the mail body after clicking reply or replyall in an email, the link I want to attach added to the mail in the inbox, not to the reply screen. If I click reply and open in a new tab, there is no problem because the inspector is not null. In short, when I click the reply mail, I cannot get the correct mailItem object how can i get right one?

Microsoft.Office.Interop.Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();

if (inspector == null)
{
    mi = (MailItem)Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];

    if (mi == null)
    {
        return;
    }

}
else
{
    mi = (MailItem)inspector.CurrentItem;
}

I'm using this code to get mailItem.

enter image description here

I'm trying to add the link to the green area in the image above but it changes the body of the email in the inbox and adds it there.

enter image description here

As you can see, the link has been added to the body of the previously received email in the inbox.


Solution

  • If you need the inline response, use Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse

    But you can have both a standalone inspector and an inline response. You really need to check what Globals.ThisAddIn.Application.ActiveWindow returns, Inspector or Explorer.