简体   繁体   English

Outlook插件可替换电子邮件中的文本

[英]Outlook plugin to replace text in emails

Visual Studio 2015. Outlook 2016. Visual Studio2015。Outlook2016。

I want to write a plug-in that replaces certain forms of text with a hyperlink while looking at the email in either the reading pane or an Inspector. 我想编写一个在阅读窗格或检查器中查看电子邮件时用超链接替换某些形式的文本的插件。

I can subscribe to the ItemLoad event: 我可以订阅ItemLoad事件:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(OnItemLoad);
    }

    private void OnItemLoad(object item)
    {
        Outlook.MailItem mailItem = item as Outlook.MailItem;

        if (mailItem != null)
        {
            System.Diagnostics.Debug.WriteLine("OnItemLoad: " + mailItem.Subject);
        }
    }

But when it is fired for some reason I can't access anything on the Outlook.MailItem instance. 但是,由于某种原因将其触发时,我无法访问Outlook.MailItem实例上的任何内容。 I get the following exception: 我得到以下异常:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in FirstOutlookAddIn.dll but was not handled in user code FirstOutlookAddIn.dll中发生类型'System.Runtime.InteropServices.COMException'的异常,但未在用户代码中处理

Additional information: The item's properties and methods cannot be used inside this event procedure. 附加信息:该项目的属性和方法不能在此事件过程中使用。

Thanks in advance! 提前致谢!

That error message is very unambiguous - no OOM properties or methods can be accessed in some event handlers. 该错误消息非常明确-在某些事件处理程序中无法访问OOM属性或方法。

One workaround would be to wait until you are out of the event handler - either use a different event (if available), or enable a timer in the OOM event handler, then in the timer event handler do what you need to do (you will be out of the OOM event handler by the time it fires). 一种解决方法是等待直到您退出事件处理程序-使用其他事件(如果可用),或在OOM事件处理程序中启用计时器,然后在计时器事件处理程序中执行所需的操作(您将在触发时已退出OOM事件处理程序)。 Use the Timer class from the Forms namespace as it fires on the main thread. 在主线程上触发时,使用Forms命名空间中的Timer类。

Keep in mind however that it is not a good idea to modify existing items - chances are the changes will persisted (also updating the last modified date) or that Outlook will prompt the user to save the changes. 但是请记住,修改现有项目不是一个好主意-更改可能会持续存在(还会更新上次修改日期),或者Outlook会提示用户保存更改。

Try to work with the Word editor exposed through Inspector.GetWordEditor . 尝试使用通过Inspector.GetWordEditor公开的Word编辑Inspector.GetWordEditor For the reading pane, you can use the ReadingPane object in Redemption . 对于阅读窗格,可以使用Redemption中ReadingPane对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM