pixel
pixel

Reputation: 10557

Outlook VSTO - Add new line to WordEditor Document Hyperlink

I am using following code to add hyperlinks to my MailItem

object link = url + System.Environment.NewLine;

Microsoft.Office.Interop.Outlook.MailItem currentMessage = MyAddIn.Application.ActiveInspector().CurrentItem;
Microsoft.Office.Interop.Word.Document doc = currentMessage.GetInspector.WordEditor;
Microsoft.Office.Interop.Word.Selection sel = doc.Windows[1].Selection;
doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);

While this does insert each link on new line in Outlook MailItem, it also shows a wired character in front of each new line starting the 2nd line: enter image description here

UPDATE: I have also tried adding it to selection range like

sel.Range.Text = System.Environment.Newline;

, but that was not adding new line at all.

Upvotes: 0

Views: 955

Answers (1)

joeschwa
joeschwa

Reputation: 3175

Because Outlook uses the Microsoft Word editor to compose messages, Word's object model applies here. sel in your code represents the Selection object, which is the location of the insertion point in the message. Redefining the selection point after each hyperlink insertion let's Word know where to put the link.

doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);
sel.EndKey(Word.WdUnits.wdLine); 
sel.InsertAfter("\n");
sel.MoveDown(Word.WdUnits.wdLine);

Environment.NewLine is supposed to get the newline string as defined for the particular environment being used, but clearly does not work here hence "\n". And I can't think of a way to make concatenating "\n" with the URL work as link is not a string and Hyperlinks.Add requires it to be an object.

Upvotes: 1

Related Questions