c# - 从阅读窗格中获取单击的超链接
问题描述
我有一个 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
}
}
}
}
解决方案
您的算法必须是:
- 检查是否
Application.ActiveWindow
是Inspector
.Application.ActiveWindow.WordEditor
如果是,请使用。 - 否则,
Application.ActiveWindow
转换为Explorer
- 检查是否
Explorer.ActiveInlineResponseWordEditor
不为空。用它。 - 否则,使用
Explorer.PreviewPane.WordEditor
推荐阅读
- javascript - 带有母版页/模板的 React Router V4 路由器
- python - 访问参数帮助字符串中的“隐式”元变量值
- javascript - 防止 React-Select 一直为空白
- javascript - 从生成器传播收益,同时以相同的方式使用它们(重用)
- angularjs - 打字稿日期格式不起作用
- hibernate - 尝试使用 wildfly13 部署时出现“NoSuchException”异常
- ios - iOS 应用程序 - iOS 11.4 随机崩溃
- arrays - 过滤 JSON 对象
- inheritance - odoo - 在扩展模型的 xml 中包含基本模型的 xml
- asp.net - Typescript 中的动态 URL