tags:

views:

53

answers:

2

What is the best way to convert HTML into MS Word .doc in .NET?

Third party components?

+1  A: 

try using Office Interop

    private void _convetHTML2Doc( string FileNameUpload)
{
    string filePath = Server.MapPath("~/htmlfile");
    object missing = Type.Missing;
    object FileName = @"D:\" + "\\" + FileNameUpload;
    object readOnly = true;
    m_word = new Application();

    m_word.Documents.Open(ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    string newfilename = filePath + "\\" + FileNameUpload.Replace(".html", ".doc");
    object o_newfilename = newfilename;

    object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;

    object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

    object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
    m_word.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref o_encoding, ref missing, ref missing, ref o_endings, ref missing);
    m_word.Quit(ref missing, ref missing, ref missing);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_word);
    }
Vinay B R
Automating Office on a server is a _bad_ idea.
SLaks
@SLaks: I don't think the OP said they were doing this on a server...
Otaku
@Otaku: The answer is. (`Server.MapPath`)
SLaks
+1  A: 

MS warns against automating Word when it's not being watched/supervised/controlled by an end-user, e.g. because it may pop a message box; so if you want to do this on a server then a 3rd party component may be better than automating Word.

You can also learn to write the Word document format yourself (it's a documented XML format), but that (learning and writing that format) is probably more trouble than you want.

Also, note that Word can open HTML: so to some (perhaps slight) extent, HTML already is a Word document.

ChrisW