【问题标题】:Outlook VSTO - Adding file path/hyperlink to MailItem WordEditor from Outlook VSTOOutlook VSTO - 从 Outlook VSTO 将文件路径/超链接添加到 MailItem WordEditor
【发布时间】:2016-12-19 18:34:58
【问题描述】:

我遇到以下问题。我有一个打开的窗口,允许我选择一些文件。 然后我可以右键单击该窗口并选择将所选文件的路径附加到新的邮件对话框。

工作流程是这样的:

  1. 我打开窗口并选择几个文件

  2. 右键,选择将选中的文件路径添加到MailItem

  3. 逻辑会检查是否有ActiveInspector

    3.1。如果有,我会得到它的CurrentItem as MailItem(因此,存在新的邮件对话框,不需要创建)

    3.2。如果没有,我打电话给CreateItem(Microsoft.Office.Interop.OLItemType.olMailItem) 创建 新邮件对话框,然后我调用MailItem.Display(false) 显示 邮件项目对话框

  4. 接下来,我将遍历选定文件路径的列表并将它们添加到新邮件对话框中。这很好用。

问题如果我第二次打开我的窗口以选择更多文件并将它们的路径添加到我之前打开的同一个邮件对话框中,它们不会被添加。

代码如下:

public void AddFilePaths(List<string> paths)
{
    if (paths.Count > 0)
    {
        var inspector = MyAddIn.Application.ActiveInspector();
        MailItem mi = null;
        bool newMailItem = false;

        if (inspector != null)
        {
            // If new mail dialog is already open, just get it.
            // This is called on my 2nd attempt to add paths to new mail.
            // This MailItem is the same one created on 1st call in below
            // else block.  I confirmed that by adding some dummy email
            // Body in below else block, then checking for it here on 
            // 2nd call.  I think this proves that correct 
            // Inspector/MailItem is returned here.
            mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
        }
        else
        {
            // create new mail dialog and display it
            // this is called on my 1st call to add paths to new mail
            mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            mi.Body = "Dummy email body"; 
            newMailItem = true;
        }

        if (newMailItem)
        {
            mi.Display();
            inspector = MyAddIn.Application.ActiveInspector();
        }

        if (inspector != null)
        {
            foreach (var path in paths)
            {
                AddPathToActiveInspector(path);
            }
        }
    }
}

上面的代码调用这个方法来添加当前ActiveInspectorWordEditor的路径:

public void AddPathToActiveInspector(string path)
{
    var inspector = MyAddIn.Application.ActiveInspector();
    dynamic we = inspector.WordEditor;
    dynamic word = we.Application;
    const string nl = "\n";

    // I have noticed that if I am debugging, this line will throw error
    // "COMException was unhandled by user code", "An exception of type
    // System.Runtime.Interop.Services.COMException occurred in 
    // System.Dynamic.dll but was not handled by user code:
    // Message: This command is not available
    // InnerException: null
    // I have also seen following error on 2nd attempt: "The TypeText    
    // method or property is not available because the document is
    // locked for editing."
    word.Selection.TypeText(nl);

    string address = path;
    string subAddress = "";
    string screenTip = "";
    string displayText = path; 
    word.ActiveDocument.Hyperlinks.Add(word.Selection.Range, ref address, ref subAddress, ref screenTip, ref displayText);
    word.Selection.TypeText(" "); 
}

【问题讨论】:

  • 那么如果你单步执行你的代码会发生什么?它真的运行吗?你确定只有一名督察吗?您如何处理内联回复
  • 谢谢 Dmitry,请查看我更新的问题。我已经跟踪了代码,我得到了正确的检查器/MailItem,我通过在创建 MailItem 时添加虚拟 MailItem.Body 消息确认(在第一次调用时),然后在第二次调用时检查正文中的该文本。我将其添加到我上面的代码注释中。我还发现一个 COMException 被抛出,但仅在调试时(???)。我也在我的代码中添加了注释。在我的回复和转发部分(内联回复)中,没有检查任何内容,并且我包含原始消息文本(如果这是您对内联回复的意思。非常感谢

标签: c# outlook vsto mailitem


【解决方案1】:

每次您添加路径时,我都会创建一封新电子邮件,以避免向错误电子邮件添加路径的可能性,如果您打开了多封电子邮件,可能会发生这种情况。

  1. 将Microsoft.Office.Interop.Word 的引用添加到您的项目中
  2. 在类文件顶部使用Microsoft.Office.Interop.Word 添加

代码如下:

public void AddFilePaths(List<string> paths)
{
    if (paths.Count > 0)
    {
        MailItem  mi = ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        mi.Display();
        if (mi!= null)
        {
            foreach (var path in paths)
            {
                AddPathsToNewEmailMessage(path);
            }
        }
    }
}

上面的代码调用这个方法来添加新邮件WordEditor的路径:

public void AddPathsToNewEmailMessage(string path)
{
    object link = url;
    object result = "url";
    object missing = Type.Missing;
    string nl = "\n";

    var inspector = ThisAddIn.Application.ActiveInspector();
    MailItem currMessage = inspector.CurrentItem;
    Word.Document doc = currMessage.GetInspector.WordEditor;
    Word.Selection sel = doc.Windows[1].Selection;
    doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);
    sel.EndKey(Word.WdUnits.wdLine);
    sel.InsertAfter(nl);
    sel.MoveDown(Word.WdUnits.wdLine);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2011-05-10
    • 1970-01-01
    • 2017-11-18
    • 2013-08-05
    • 2011-12-30
    • 2016-02-09
    • 2015-05-20
    相关资源
    最近更新 更多