2

I have a ribbon button that inserts text into an outlook Inspector by modifying the MailItem object based on the recipients in it. The method that gets called on click looks like this:

public async void OnTemplateClick(Office.IRibbonControl control)
        {
            var templateId = control.Tag;
            var template = templates.GetTemplateById(templateId);
            await templateUi.SetTemplate(control.Context, template);
        }

the SetTemplate method looks like this:

public async Task SetTemplate(object window, Template template, 
            SynchronizationContext uiThread = null)
{
Outlook.MailItem currentMailItem = null;
            Outlook.Recipients olRecps = null;
            Outlook.Recipient recp = null;
            Outlook.AddressEntry addEntry = null;
            try
            {
                currentMailItem = GetMailItem(window);
                olRecps = currentMailItem.Recipients;
                var recipType = Outlook.OlMailRecipientType.olTo;
                var recps = from r in olRecps.Cast<Outlook.Recipient>()
                            where r.Type == (int)recipType
                            select r;
                var numRecps = recps.Count();

                var oldBodyHtml = currentMailItem.HTMLBody;
               ...

Now, sometimes, that last line that fetches the HTMLBody throws the following error:

System.Runtime.InteropServices.COMException (0x8E604001): Not implemented.
   at Microsoft.Office.Interop.Outlook._MailItem.get_HTMLBody()

This error doesnt happen all the time and its very difficult to reproduce so we mostly see it in the application logs. I was wondering what could possibly be causing this error? I assumed this has something to do with the timing of this asynchronous call which means that the MailItem message isnt fully formed?

Thanks!

4

1 回答 1

4

Outlook 对象模型不能用于辅助线程。Outlook 2016 将在检测到此类调用时立即引发错误。在旧版本的 Outlook 中,调用可能会意外失败。

如果必须使用辅助线程,您唯一的选择是扩展 MAPI(C++ 或 Delphi)或Redemption(其RDO对象系列可用于辅助线程)。

于 2016-03-07T20:52:24.753 回答