C# Outlook 2010插件-将短语更改为链接而不更改电子邮件正文

C# Outlook 2010插件-将短语更改为链接而不更改电子邮件正文,c#,outlook,office-interop,outlook-addin,outlook-2010,C#,Outlook,Office Interop,Outlook Addin,Outlook 2010,我已经为Outlook 2010创建了简单的加载项,用链接替换特定短语,例如,我想用ABC12345替换http://www.example.com?id=123456 我正在使用以下代码: private void ThisAddIn_Startup(object sender, System.EventArgs e) { inspectors = this.Application.Inspectors; inspectors.NewInspector += new Outlo

我已经为Outlook 2010创建了简单的加载项,用链接替换特定短语,例如,我想用
ABC12345
替换
http://www.example.com?id=123456

我正在使用以下代码:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    inspectors = this.Application.Inspectors;
    inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}

void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
    var mailItem = Inspector.CurrentItem as Outlook.MailItem;
    if (mailItem != null)
    {
        if (mailItem.EntryID != null)
        {
            string pattern = @"ABC(?<number>\d{1,6})";

            var match = Regex.Match(mailItem.HTMLBody, pattern);
            if (match.Success)
            {
                var replaced = Regex.Replace(mailItem.HTMLBody, pattern, m => "<a href=\"http://example.com?query=" + m.Groups["number"].Value+"\">ABC"+ m.Groups["number"].Value+"</a>");
                mailItem.HTMLBody = replaced;
            }      
        }
    }
}
EDIT2
我可以在Outlook中获取WordEditor,但无法添加超链接。 我现在想做的最后一件事是在电子邮件中找到特定的短语并添加超链接。这是我目前的代码:

Outlook.Explorer currentExplorer;

private void ThisAddIn_Startup(object sender, EventArgs e)
{
    currentExplorer = this.Application.ActiveExplorer();
    currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
}

private void CurrentExplorer_Event()
{
    var selectedFolder =  this.Application.ActiveExplorer().CurrentFolder;
    try
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            var selectedObject = this.Application.ActiveExplorer().Selection[1];
            if (selectedObject is Outlook.MailItem)
            {
                var mail = (selectedObject as Outlook.MailItem);
                var inspector = mail.GetInspector;

                if (inspector.IsWordMail())
                {
                    var document = (Document)inspector.WordEditor;

                    if (document != null)
                    {
                        var app = document.Application;
                        var find = document.Range().Find;

                        find.Text = "ABC";
                        while (find.Execute())
                        {
                            var rng = find.Parent as Range;
                            rng.Hyperlinks.Add(rng, "http://example.com/?query=" + rng.Text );
                            rng.Collapse(WdCollapseDirection.wdCollapseEnd);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

你需要更具体地说明你需要什么。此行mailItem.HTMLBody=已替换;似乎在做你不想做的事。“替换”怎么办?@AlexandruClonțea我想将文本
ABC123456
更改为链接。我试图替换电子邮件正文,但Outlook这样认为我在编辑电子邮件。我想让电子邮件保持原样,但我希望该文本是链接。希望这是有意义的:)那么,把文本作为链接留给谁呢?我很困惑。听起来很像你在试图改变邮件的正文:)你是指邮件显示给的人吗?还有,为什么这听起来很恶意:)@AlexandruClonțea当我想到它时,它可能听起来很恶意:)在我的工作中,我们有一个非常旧的帮助台系统,它在电子邮件通知中发送任务编号,而不是链接,这对我来说是黑盒,(很遗憾)我无法更改该模板。因为我们使用的是Outlook 2000+,所以我想我可以创建一个插件,将静态文本更改为可单击的链接。这是我的主要意图。如果你认为这是恶意的,那么我很抱歉,但这不是我的本意。更简单地说,要求似乎是在Outlook没有注意到的情况下默默地更改电子邮件正文。
Outlook.Explorer currentExplorer;

private void ThisAddIn_Startup(object sender, EventArgs e)
{
    currentExplorer = this.Application.ActiveExplorer();
    currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
}

private void CurrentExplorer_Event()
{
    var selectedFolder =  this.Application.ActiveExplorer().CurrentFolder;
    try
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            var selectedObject = this.Application.ActiveExplorer().Selection[1];
            if (selectedObject is Outlook.MailItem)
            {
                var mail = (selectedObject as Outlook.MailItem);
                var inspector = mail.GetInspector;

                if (inspector.IsWordMail())
                {
                    var document = (Document)inspector.WordEditor;

                    if (document != null)
                    {
                        var app = document.Application;
                        var find = document.Range().Find;

                        find.Text = "ABC";
                        while (find.Execute())
                        {
                            var rng = find.Parent as Range;
                            rng.Hyperlinks.Add(rng, "http://example.com/?query=" + rng.Text );
                            rng.Collapse(WdCollapseDirection.wdCollapseEnd);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}