Accessing Mail Item when in Inline Response mode in Outlook

Add-in Express™ Support Service
That's what is more important than anything else

Accessing Mail Item when in Inline Response mode in Outlook
Difficulty accessing Mail Item when in Inline Response mode using OutlookApp.ActiveInspector 
SmartOutcome




Posts: 5
Joined: 2016-08-23
OutlookApp.ActiveInspector returns null when in Inline Response mode in Outlook. What do I need to do to access my current email response here? I need to parse an email and provide template information. Please note that the code works fine when not in Inline Response mode.
Posted 23 Aug, 2016 10:55:50 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
SmartOutcome writes:
OutlookApp.ActiveInspector returns null when in Inline Response mode in Outlook.


This is because there's no inspector opened. You need to use the Explorer.ActiveInlineResponse property instead; see https://msdn.microsoft.com/en-us/library/office/jj231535(v=office.15).aspx.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Aug, 2016 01:45:04 Top
SmartOutcome




Posts: 5
Joined: 2016-08-23
Hi Andrei,

I managed to get access to the Explorer.ActiveInlineResponse property and hence the response MailItem object. However, I have a new challenge; when I write into the MailItem.HTMLBody the text gets resent back to the original HTMLBody thereafter. My goal is to append new text to the response while keeping the original email text.

Do you know what is happening here?

Regards
Posted 07 Oct, 2016 07:37:59 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello,

Do you save the item after setting HTMLBody?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Oct, 2016 08:53:54 Top
SmartOutcome




Posts: 5
Joined: 2016-08-23
Hi Andrei

Here is the code snippet:


        private void PopulateEmailFields(bool isInspectorNeeded = false)
        {
            Inspector currInspector = null;
            MailItem currMail = null;
            Explorer explorer = null;
            MailItem response = null;

            try
            {
                if (isInspectorNeeded)
                {
                    explorer = OutlookApp.ActiveExplorer();
                    response = explorer.GetType().InvokeMember("ActiveInlineResponse",
                        BindingFlags.GetProperty |
                        BindingFlags.Instance |
                        BindingFlags.Public,
                        null, explorer, null) as MailItem;

                    if (response == null)
                    {
                        return;
                    }

                    currInspector = response.GetInspector;
                }
                else
                {
                    currInspector = OutlookApp.ActiveInspector();
                }

                if (CheckDetailsAvailable(currInspector))
                {
                    return;
                }

                currMail = currInspector.CurrentItem as MailItem;

                if (currMail == null)
                {
                    return;
                }

                // add recipients address
                currMail.To = foundCustomer.Email;

                var strHtml = MergeDetailsIntoEmailBody(FoundTemplate, foundCustomer);

                if (string.IsNullOrEmpty(currMail.HTMLBody))
                {
                    currMail.HTMLBody = strHtml;

                    currMail.Subject = FoundTemplate.Subject;
                }
                else
                {
                    // we need to append the text from the template and not destroy the current email
                    var strHtmlBody = currMail.HTMLBody;

                    var intTagStart = Strings.InStr(1, strHtmlBody, "<body", CompareMethod.Text);
                    var intTagEnd = Strings.InStr(intTagStart + 5, strHtmlBody, ">");
                    var strBodyTag = Strings.Mid(strHtmlBody, intTagStart, intTagEnd - intTagStart + 1);

                    var newBody = Strings.Replace(strHtmlBody, strBodyTag, strBodyTag + strHtml);

                    currMail.Body = string.Empty;
                    currMail.HTMLBody = newBody;
                    currMail.Save();
                }

                // add any attachments
                DoAddAttachments(currMail, FoundTemplate, foundCustomer);
            }
            catch (Exception ex)
            {
                var str = ex.Message;
            }
            finally
            {
                if (currMail != null) Marshal.ReleaseComObject(currMail);
                if (currInspector != null) Marshal.ReleaseComObject(currInspector);
                if (explorer != null) Marshal.ReleaseComObject(explorer);
                if (response != null) Marshal.ReleaseComObject(response);
            }
        }
Posted 07 Oct, 2016 09:08:13 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
        if (string.IsNullOrEmpty(currMail.HTMLBody)) 
        { 
            currMail.HTMLBody = strHtml; 
            ... 
        } 
        else 
        { 
            ...
            currMail.Body = string.Empty; 
            currMail.HTMLBody = newBody; 
            currMail.Save(); 
        } 


Note that you don't save the email if the condition is true. And I would comment out the line which sets the Body property.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Oct, 2016 09:47:02 Top