Evgenyt
Evgenyt

Reputation: 10721

Convert HTML to Microsoft Word .doc in .NET

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

Third party components?

Upvotes: 1

Views: 2454

Answers (2)

ChrisW
ChrisW

Reputation: 56083

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.

Upvotes: 1

Vinay B R
Vinay B R

Reputation: 8421

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

Upvotes: 1

Related Questions