none
Outlook 2010 / VB.net: How do I insert HTML text into a WordEditor at its current selection? RRS feed

  • Question

  • I 'm using VB.net 2010 to build a COM addin that targets Outlook 2010.

    I have an Outlook.PostItem with BodyFormat = Outlook.OlBodyFormat.olFormatHTML.

    I have a WordEditor that contains a message that is being composed as a reply or forward to another message. The message being composed is in olFormatHTML as well.

    I would like to insert the HTML contents (<body></body> only) of the PostItem into the WordEditor at the current selection.

    I know how to do this with a simple text message:

    WordEditor.Application.Selection.Text = PostItem.Body

    I would like to do this:

    Dim insertHtmlText As String = GetBodyFromHtmlPage(PostItem.HTMLBody)
    WordEditor.Application.Selection.HtmlText = insertHtmlText

    How do I insert HTML text into a WordEditor at its current selection?

    Thanks

    Monday, June 6, 2011 12:06 AM

Answers

  • No, it looks like it doesn't accept HTML markup. That example is just showing up as raw HTML in the email body.
     
    From further testing it appears that there are a couple of workarounds that would need to be used.
     
    If it wasn't a requirement to insert the text at the current selection you could just work with the HTMLBody and set that. To add HTML in a specific place in the HTML you could get the mail item's HTMLBody and use string functions to insert your markup where desired. To get the insertion point you could get WordEditor.Application.Selection.Text I suppose.
     
    Something like that might work.
     
    Another possibility would be to put your markup in a Word doc using automation code and put that on the clipboard using Copy() and then call PasteSpecial() on the email document.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ou8" <=?utf-8?B?T3U4?=> wrote in message news:08fa4202-7291-4a44-8869-c1364a3aad1e...

    Thanks.

    So InsertAfter accepts HTML markup?

    As in:

    WordEditor.Application.Selection.InsertAfter "<p>This <strong>is</strong> a paragraph</p>"

     


    Ken Slovak MVP - Outlook
    Tuesday, June 7, 2011 1:37 PM
  • Where I've done templates like that I've used .htm files with special field delimiters around text to replace. That was specific to templates with embedded text and images stored in file system folders.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ou8" <=?utf-8?B?T3U4?=> wrote in message news:c64c9daa-19f6-4958-9e89-67e889f86770...

    Thanks & Yikes.

    WordEditor.Application.Selection.Text would be null if nothing is selected (eg I just have an insert cursor blinking in the WordEditor) so I would have nothing to search for in the HTMLBody.

    I suppose I could insert some text myself (WordEditor.Application.Selection.Text = "xxxYYYxxx") and then search for that and replace it with the HTML markup I want to insert.

    I'm working on a template type of COM add-in that allows the user to insert the contents of various items stored in a folder structure within Outlook.


    Ken Slovak MVP - Outlook
    Tuesday, June 7, 2011 10:20 PM

All replies

  • I believe you'd use Selection.InsertAfter.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ou8" <=?utf-8?B?T3U4?=> wrote in message news:f1801ac6-c9c4-4569-bab1-273701cfc577...

    I 'm using VB.net 2010 to build a COM addin that targets Outlook 2010.

    I have an Outlook.PostItem with BodyFormat = Outlook.OlBodyFormat.olFormatHTML.

    I have a WordEditor that contains a message that is being composed as a reply or forward to another message. The message being composed is in olFormatHTML as well.

    I would like to insert the HTML contents (<body></body> only) of the PostItem into the WordEditor at the current selection.

    I know how to do this with a simple text message:

    WordEditor.Application.Selection.Text = PostItem.Body

    I would like to do this:

    Dim insertHtmlText As String = GetBodyFromHtmlPage(PostItem.HTMLBody)
    WordEditor.Application.Selection.HtmlText = insertHtmlText

    How do I insert HTML text into a WordEditor at its current selection?

    Thanks


    Ken Slovak MVP - Outlook
    Monday, June 6, 2011 2:10 PM
  • Thanks.

    So InsertAfter accepts HTML markup?

    As in:

    WordEditor.Application.Selection.InsertAfter "<p>This <strong>is</strong> a paragraph</p>"

     

    Tuesday, June 7, 2011 1:02 AM
  • No, it looks like it doesn't accept HTML markup. That example is just showing up as raw HTML in the email body.
     
    From further testing it appears that there are a couple of workarounds that would need to be used.
     
    If it wasn't a requirement to insert the text at the current selection you could just work with the HTMLBody and set that. To add HTML in a specific place in the HTML you could get the mail item's HTMLBody and use string functions to insert your markup where desired. To get the insertion point you could get WordEditor.Application.Selection.Text I suppose.
     
    Something like that might work.
     
    Another possibility would be to put your markup in a Word doc using automation code and put that on the clipboard using Copy() and then call PasteSpecial() on the email document.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ou8" <=?utf-8?B?T3U4?=> wrote in message news:08fa4202-7291-4a44-8869-c1364a3aad1e...

    Thanks.

    So InsertAfter accepts HTML markup?

    As in:

    WordEditor.Application.Selection.InsertAfter "<p>This <strong>is</strong> a paragraph</p>"

     


    Ken Slovak MVP - Outlook
    Tuesday, June 7, 2011 1:37 PM
  • Thanks & Yikes.

    WordEditor.Application.Selection.Text would be null if nothing is selected (eg I just have an insert cursor blinking in the WordEditor) so I would have nothing to search for in the HTMLBody.

    I suppose I could insert some text myself (WordEditor.Application.Selection.Text = "xxxYYYxxx") and then search for that and replace it with the HTML markup I want to insert.

    I'm working on a template type of COM add-in that allows the user to insert the contents of various items stored in a folder structure within Outlook.

    Tuesday, June 7, 2011 5:48 PM
  • Where I've done templates like that I've used .htm files with special field delimiters around text to replace. That was specific to templates with embedded text and images stored in file system folders.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ou8" <=?utf-8?B?T3U4?=> wrote in message news:c64c9daa-19f6-4958-9e89-67e889f86770...

    Thanks & Yikes.

    WordEditor.Application.Selection.Text would be null if nothing is selected (eg I just have an insert cursor blinking in the WordEditor) so I would have nothing to search for in the HTMLBody.

    I suppose I could insert some text myself (WordEditor.Application.Selection.Text = "xxxYYYxxx") and then search for that and replace it with the HTML markup I want to insert.

    I'm working on a template type of COM add-in that allows the user to insert the contents of various items stored in a folder structure within Outlook.


    Ken Slovak MVP - Outlook
    Tuesday, June 7, 2011 10:20 PM