c# - 如何更改在预览 Pane 中显示或打开的 MailItem

标签 c# outlook ms-office outlook-addin office-2010

我正在尝试编写一个加载项,该加载项可以在加载 Outlook MailItem 时对其进行修改(通过预览 Pane 或通过打开它)。这是我到目前为止的代码,但我似乎无法实际访问 MailItem

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        Application.ItemLoad += ApplicationOnItemLoad;
    }

    private void ApplicationOnItemLoad(object item)
    {
        var mail = item as MailItem;

        if (mail != null)
        {
            Console.WriteLine(mail.HTMLBody);
        }
    }

    // Etc...
}

代码到达 Console.WriteLine(mail.HTMLBody); 语句就很好,这意味着将项目转换为 Outlook MailItem 没问题(它最终不会为空)。但是,我无法访问任何 MailItem 成员...它只会引发异常:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in MyOutlookProject.DLL
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

最佳答案

Application.ItemLoad发生在元素完全装载之前。除了 ClassMessageClass 之外,您无法读取 Item 上的任何属性。

来自 MSDN...

This event occurs when the Outlook item begins to load into memory. Data for the item is not yet available, other than the values for the Class and MessageClass properties of the Outlook item, so an error occurs when calling any property other than Class or MessageClass for the Outlook item returned in Item.

要访问预览 Pane 中的 MailItem,您应该查看 Explorer.Selection 。要访问已打开的 MailItem(通过检查器) - 利用 Inspectors.NewInspector .

关于c# - 如何更改在预览 Pane 中显示或打开的 MailItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476273/

相关文章:

c# - 如何在 UWP 中隐藏 MediaPlayer 控件

c# - 为什么我在 tablelayoutpanel 中有垂直滚动条

HTML 电子邮件 Outlook 2013 120DPI 背景

html - Outlook 2007 图像对齐问题

vba - 如果只能存在一个错误对象,那么声明 ErrObject 变量有什么用?

c# - 如何部署 ASP.NET 项目以使 .aspx 文件中没有代码?

c# - DllImport vs LoadLibrary,最好的方法是什么?

java - apache POI MS Office Excel 无法打开字体颜色设置 Excel 文件

python - 在 python 3 中“尝试” 'duration'

docker - Microsoft Word 在 Docker 容器中调用其 COM 时崩溃