tags:

views:

117

answers:

2

I'd like to extract the content of a MS Word 2003 document into HTML in C#.

Any ideas?

+3  A: 

I think this is the easiest way to do it

http://asptutorials.net/C-SHARP/convert-ms-word-docs-to-html/

They key point in the article is that they use the SaveAs function http://msdn.microsoft.com/en-us/library/aa220734.aspx

Like this:

    string newfilename = folder_to_save_in + FileUpload1.FileName.Replace(".doc", ".html");
    object o_nullobject = System.Reflection.Missing.Value;    
    object o_newfilename = newfilename;
    object o_format = Word.WdSaveFormat.wdFormatHTML;
    object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
    object o_endings = Word.WdLineEndingType.wdCRLF;
    // SaveAs requires lots of parameters, but we can leave most of them empty:
    wordApplication.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref o_nullobject,
    ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
    ref o_nullobject, ref o_nullobject, ref o_encoding, ref o_nullobject,
    ref o_nullobject, ref o_endings, ref o_nullobject);

The library is Microsoft.Office.Interop.Word;

If I remember correctly Word is required on the machine where the code is executed. If it's ASP.NET it is required on the server.

The real napster
I think you forgot to assign/declare o_nullobject in your sample. It should be set to `System.Type.Missing`
0xA3
thanks divo, forgot it yes.
The real napster
A: 

Three ways: 1. save as HTML, as described by napster 2. transform the Open XML to HTML; the XSLT is available at http://www.codeplex.com/OpenXMLViewer 3. for the cleanest HTML, write code to convert each style in the document to CSS, and put any direct formatting in @style.

Is Word installed on the computer running your C# code?

plutext
Word is installed on the computer, it's not an issue.With napster's solution, I got some realy messy html output. I'm sure someone has allready written all the code to clean that html?
Xavier