问题
I'm trying to make a context menu for a hyperlink. It seems there are several contexts where hyperlink events can be intercepted -- at the moment I'm interested in the context menu with idMso="ContextMenuReadOnlyMailHyperlink".
There are two different ways the callback for my new button in this menu can be executed -- if hyperlink is right-clicked in the preview pane of Outlook, or if an email is opened for reading in its own window.
When invoked from preview pane, my callback receives an Explorer COM object. I assumed the Explorer's ActiveInlineResponseWordEditor property would contain my selected hyperlink somehow, but it throws a non-helpful COM exception ("The operation failed").
How can I find the selected hyperlink from this context?
Also, how about for the Inspector COM object (when hyperlink is right-clicked from email in its own window)?
回答1:
I assumed the Explorer's ActiveInlineResponseWordEditor property would contain my selected hyperlink somehow, but it throws a non-helpful COM exception ("The operation failed").
The ActiveInlineResponseWordEditor property cannot be used when the InlineResponse is not activated. The InlineResponse event of the Explorer class is fired when the user performs an action that causes an inline response to appear in the Reading Pane. In your case the inline response is not activated.
How can I find the selected hyperlink from this context?
The Explorer class provides the Selection property which returns a Selection object that contains the item or items that are selected in the explorer window. You can use the Item method (represented by the indexer in C#) to get a Microsoft Outlook item or conversation header from the selection. Then try to cast it to the MailItem class and get the Inspector object, see the GetInspector method of the MailItem class. The Inspector class provides the WordEditor property which returns the Microsoft Word Document Object Model of the message being displayed. You can use the Word object model to get the selection.
回答2:
I had a similar problem and my solution looked like:
public void OnCustomHyperlinkMenuClick(IRibbonControl control)
{
Explorer explorer = control.Context as Explorer;
if (explorer != null)
{
Document document = explorer.ActiveInlineResponseWordEditor;
//Note from asker: above line throws a COM Exception ("The operation failed")
if (document != null && document.Windows != null && document.Windows.Count > 0)
{
Microsoft.Office.Interop.Word.Selection selection = document.Windows[1].Selection;
if (selection != null && selection.Hyperlinks != null && selection.Hyperlinks.Count > 0)
{
Hyperlink hyperlink = selection.Hyperlinks[1];
string hyperlinkUrl = hyperlink.Address;
DoSomethingWithUrl(hyperlinkUrl);
}
}
}
}
You will need to add a reference to the word interop assembly "Microsoft.Office.Interop.Word.dll" to your project.
来源:https://stackoverflow.com/questions/28509147/outlook-hyperlink-context-menu