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 contex
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.
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.