Inserting HTML in Outlook

Add-in Express™ Support Service
That's what is more important than anything else

Inserting HTML in Outlook
Attempting to insert HTML in the body 
German Hayles




Posts: 41
Joined: 2015-04-27
We have an Add-in that inserts HTML into the body of an Outlook email message.


...
mailItem.HTMLBody = //Some HTML  <DocType .><html> .... </html>


Everything renders properly.

I was just handed a requirement that the HTML should be inserted where ever the cursor happens to be located in the message body.

We're well aware about the unusual nature of this request. I'd be curious to see how this would be done, if possible.

My first thought was to use the "WordEditor" object of the Inspector and cast it to a "Document" object. This didn't work because regardless of which "InsertXXX" function (e.g. InsertAfter), the HTML was rendered as plain text (You saw the literal HTML; tags and all)

Is there are REASONABLE way of accomplishing this?
Posted 31 Mar, 2017 02:43:19 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello German,

Put the HTML in the Clipboard and then try to get Inspector.WordEditor, cast it to Word.Document, get Document.Windows(1), Window.Selection, Selection.Range, and finally, use Range.PasteSpecial(Type.Missing, Type.Missing, Type.Missing, Word.WdPasteDataType.wdPasteHTML).


Andrei Smolin
Add-in Express Team Leader
Posted 31 Mar, 2017 03:01:21 Top
German Hayles




Posts: 41
Joined: 2015-04-27
Thank for the response.

I got the following error:

System.Runtime.InteropServices.COMException (0x800A14DE): The specified data type is unavailable.




Microsoft.Office.Interop.Word.Document doc = inspector.WordEditor as Microsoft.Office.Interop.Word.Document;
Microsoft.Office.Interop.Word.Windows wins = doc.Windows;

Microsoft.Office.Interop.Word.Window win = doc.Windows[1];

System.Windows.Forms.Clipboard.SetText(rm.GetString("HTML_FROM_EMBEDED_STRING_RESOURCE"));

win.Selection.Range.PasteSpecial(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Word.WdPasteDataType.wdPasteHTML);


I also tried Selection.Range.PasteSpecial ... but with no luck.
Posted 03 Apr, 2017 21:25:11 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello German,

Put the HTML to be pasted to the Clipboard, switch to the email inspector, on the Message tab select Paste | Paste Special, and check that the HTML Format item is available in the As: listbox. If it isn't available, you won't be able to paste that text as HTML.


Andrei Smolin
Add-in Express Team Leader
Posted 04 Apr, 2017 02:22:56 Top
German Hayles




Posts: 41
Joined: 2015-04-27
Nice piece of trouble shooting. The HTML paste option was not available when I followed your example. Now, I know.
Thanks.

I'm thinking of trying something like this

Message Body: Some Random Text

HTML Text: <html><body><table>...</table></body></html>

Now, if I want to insert the html between the words "Random" and "Text" I could try something like this

<html><body>Some Random <table>...</table>Text</body></html>


I'd merge the text within the <body> section of the HTML.

I'm not sure how painful this will be, but I'll give it a shot.

Thanks again for your help.
Posted 04 Apr, 2017 13:34:56 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hello German,

I suggest that you invest in preparing a proper HTML which Word might insert.


Andrei Smolin
Add-in Express Team Leader
Posted 05 Apr, 2017 02:34:02 Top
German Hayles




Posts: 41
Joined: 2015-04-27
Thanks Andrei.
Posted 06 Apr, 2017 12:35:33 Top
German Hayles




Posts: 41
Joined: 2015-04-27
Hello Andrei,

I found a solution on another site. The following will work


Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
Word.Document wdDoc = mailItem.GetInspector.WordEditor;
wdDoc.Application.Selection.InsertFile(FileName: @"D:.txt", Attachment: false);
Posted 11 Apr, 2017 21:59:17 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Thank you for posting this solution!


Andrei Smolin
Add-in Express Team Leader
Posted 12 Apr, 2017 04:10:44 Top