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