I am creating a C# VSTO add-in for Outlook 2010. I am attempting to generate a hyperlink at the insertion point of the active outgoing message that is being worked on (the hyperlink is inserted via a button on the message window ribbon). All other functions of the add-in (ribbon button, accessing the ActiveInspector().CurrentItem
, etc.) work fine. I am working with this code:
object linktext = txtDisplayText.Text;
object result = "MY URL";
object missObj = Type.Missing;
Outlook.MailItem currentMessage =
Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
Word.Document doc = currentMessage.GetInspector.WordEditor;
object oRange = doc.Windows[1].Selection;
doc.Application.Selection.Hyperlinks.Add
(oRange, ref result, ref missObj, ref missObj, ref linktext, ref missObj);
When I run this code I get the message "Command failed." I suspect I am missing something either in regards to how Outlook uses Microsoft Word's editor for Outlook messages or in the way I have specified the selection object in oRange
. Any help is very much appreciated.