Click here to Skip to main content
15,170,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to insert some HTML-styled text into a Word 2007 document. As far as I know you cannot directly insert HTML into Word, because it needs to be converted first. So I save the HTML as a temporary file and use InsertRange to insert it into my document.

Here is my code:
string TempFileSaveDir = "c:\\word";

private void InsertHTML(Document pDoc, string pHTML)
    Guid _guid = Guid.NewGuid();

    string _fileName = TempFileSaveDir + "\\" + _guid.ToString() + ".html";
    TextWriter tw = new StreamWriter(_fileName, false, Encoding.UTF8);            

    object start = 0;
    object end = 0;
    object ConfirmConversions = false;
    object Link = false;
    object Attachment = false;
    object missing = System.Type.Missing;

    Word.Range _range1 = pDoc.Range(ref start, ref end);
    _range1.InsertFile(_fileName, ref missing, ref ConfirmConversions, ref Link, ref Attachment);


Now my problem is, that Word sees it as regular text rather than HTML, so if I have <b>Some Text</b> in my HTML it'll show the B tags instead of bolded text.
I noticed that when I set ConfirmConversions to true I get a popup, where I can select the HTML conversion. Is there a way to have the converter automaticly use this conversion without prompting the user every time?


This looks like a word for word copy of a post on the Microsoft Forums a month back. The Microsoft forum post is here[^]

To quote that solution:

1. You should be able to insert HTML directly because Word regards HTML
as a "native" format and has a built-in converter for it. However, Word
may not be able to convert all versions of HTML.

2. A problem (which you may or may not have) is specifying a particular
format. If you cannot do that, there is always the possibility that Word
will not recognise the format correctly. Either that is a problem for
you, or it isn't. If it is, then...

3. ...You can use Documents.Open to specify the format. However, the
best you can specify AFAIK is wdOpenFormatWebPages. If that is enough to
make Word do the right thing, then you could try something like the
equivalent of the following VBA:

Sub insertsomeHTML 
  Dim rs As Range 
  Set objDoc = ActiveDocument 

  ' set up the target range 
  Set r = objDoc.Content 
  r.Collapse Direction:=wdCollapseEnd 

  ' open the HTML 
  ' by specifying Format we avoid the 
  ' format conversion dialog 
  Set objTempDoc = Application.Documents.Open( _ 
    FileName:="c:\a\test.htm", _ 
  r.FormattedText = objTempDoc.Content 
  objTempDoc.Close savechanges:=False 
  Set objTempDoc = Nothing 
  Set r = Nothing 
  Set objDoc = Nothing 
End Sub 
>>This looks like a word for word copy of a post on the Microsoft Forums a month back

Yes, I somehow managed to miss the answer there, sorry about that.
And thank you for the help.

EDIT: it turned out that it needed to be proper HTML, while I tried to input stuff like
<div>some text <b>some bold text</b></div>

without and tags. When I added these to my HTML Word started to understand and parse it properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900