首页 > 解决方案 > 从阅读窗格中获取单击的超链接

问题描述

我有一个 Outlook VSTO 加载项,它向超链接上下文菜单(使用idMso="ContextMenuReadOnlyMailHyperlink")添加了一个按钮,我想获得单击的超链接。

感谢帖子如何在 Outlook 2010 的电子邮件中为超链接的上下文菜单添加新选项?,当邮件在新窗口中打开时,我设法获得点击的超链接。该帖子建议ActiveInlineResponseWordEditor在阅读窗格中使用邮件,但这并不适合我,因为它总是如此null

根据帖子Outlook 超链接上下文菜单ActiveInlineResponseWordEditor不能在此上下文中使用,Explorer.Selection应改为使用。我已经尝试按照该方向进行操作,但未能获得点击的超链接。Word.Selection似乎指向邮件的开头而不是指向单击的超链接,因为它返回到邮件的第一个字母。

如何从阅读窗格中获取单击的超链接?

// mail in new window
if (control.Context is Inspector inspector)
{
    Document document = inspector.WordEditor;
    if (document != null && document.Windows != null && document.Windows.Count > 0)
    {
        Microsoft.Office.Interop.Word.Selection word = document.Windows[1].Selection;
        if (word != null && word.Hyperlinks != null && word.Hyperlinks.Count > 0)
        {
            Hyperlink hyperlink = word.Hyperlinks[1];
            MessageBox.Show(hyperlink.Address); // output: the clicked hyperlink
        }
    }
}
// mail in reading pane
else if (control.Context is Explorer explorer)
{
    Microsoft.Office.Interop.Outlook.Selection selection = explorer.Selection;
    if (selection[1] is MailItem mailItem)
    {
        Inspector inspector = mailItem.GetInspector;
        Document document = inspector.WordEditor;
        if (document != null && document.Windows != null && document.Windows.Count > 0)
        {
            Microsoft.Office.Interop.Word.Selection word = document.Windows[1].Selection;
            if (word != null && word.Hyperlinks != null)
            {
                MessageBox.Show(word.Hyperlinks.Count.ToString()); // output: 0
                MessageBox.Show(word.Text); // output: the first letter of the mail
            }
        }
    }
}

标签: c#outlookvstooutlook-addin

解决方案


您的算法必须是:

  1. 检查是否Application.ActiveWindowInspector. Application.ActiveWindow.WordEditor如果是,请使用。
  2. 否则,Application.ActiveWindow转换为Explorer
  3. 检查是否Explorer.ActiveInlineResponseWordEditor不为空。用它。
  4. 否则,使用Explorer.PreviewPane.WordEditor

推荐阅读