Word 2007 automation - setting default conversion for InsertFile

G

Grzegorz Zych

Hi

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);

tw.Write(pHTML);
tw.Close();

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);

File.Delete(_fileName);
}


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?

Regards
 
P

Peter Jamieson

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", _
Format:=wdOpenFormatWebPages)
r.FormattedText = objTempDoc.Content
objTempDoc.Close savechanges:=False
Set objTempDoc = Nothing
Set r = Nothing
Set objDoc = Nothing
End Sub

Peter Jamieson

http://tips.pjmsn.me.uk
 
G

Grzegorz Zych

Thank you for the answer. The first point directed me in the right way.

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 <html> and <body> tags. When I added these to my HTML Word started
to understand and parse it properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top