防止MailItem.Reply在Outlook 2016中打开检查器窗口

时间:2018-08-29 06:20:03

标签: c# vsto outlook-addin office-addins

当用户单击“答复”按钮时,我正在为Outlook构建外接程序以在Outlook电子邮件中添加自定义签名。 用户单击浏览器中的答复按钮后,将打开一个新的检查器,并且我使用代码关闭检查器(该代码为#scks)。可能有一种方法可以完全禁止答复检查器打开。

    private Microsoft.Office.Tools.CustomTaskPane CustomTaskPane;
    UserDetail usr = null;
    Outlook.Explorer currentExplorer = null;
    Outlook.MailItem mailItem;
    private Inspector replyInspector;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange +=
                            new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
    }
    private void CurrentExplorer_Event()
    {
        if (this.Application.ActiveExplorer().Selection.Count == 1
        && this.Application.ActiveExplorer().Selection[1] is Outlook.MailItem)
        {
            if (mailItem != null)
            {
                // when the reply button is clicked
                ((Outlook.ItemEvents_10_Event)mailItem).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);

                // When an item is selected
                Outlook.Selection mySelection = this.Application.ActiveExplorer().Selection;
                Outlook.MailItem mailitem = null;
                foreach (Object obj in mySelection)
                {
                    if (obj is Outlook.MailItem)
                    {
                        mailitem = (Outlook.MailItem)obj;
                        if (mailitem != null)
                        {
                            if (mailitem.Sent)
                            {

                            else
                            {
                                // Compose
                            }
                        }
                    }
                }
            }
            mailItem = this.Application.ActiveExplorer().Selection[1];
            ((Outlook.ItemEvents_10_Event)mailItem).Reply += new
             Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
            // Close Inspector
            replyInspector.Close(OlInspectorClose.olDiscard);
        }
        else
        {
        }
    }

    void MailItem_Reply(Object response, ref bool cancel)
    {
        try
        {
            MailItem mitem = (Outlook.MailItem)response;
            replyInspector = (mitem).GetInspector;
            replyInspector.Activate();
            (mitem).HTMLBody = tempSignature + ((Outlook.MailItem)response).HTMLBody;
        }
        catch (System.Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

1 个答案:

答案 0 :(得分:1)

首先,如果您仍要打开检查器,为什么还要关闭它?为什么在打开检查器时不插入签名?

第二,您不能连接两个HTML字符串,并且不能期望结果字符串是有效的HTML。这两个需要合并。或者,更好的是,使用Word对象模型(Word.Document返回Inspector.WordEditor)在消息正文中的任何位置插入任何文本。