tags:

views:

377

answers:

5

Hello everyone,

I am looking for sample code which convert html into MSWord. C# code appreciated. Html input is as a string whose content is html document, I want to learn how to use .Net Word (Office) SDK to do the conversion.

thanks in advance, George

+1  A: 

Here is a similar question George but the links are same as my previous answer t your question about HTML editor ;-)

Shoban
+2  A: 

It depends a lot on the nature of the html document you are trying to convert. One simple way is just to use the Word automation to open an .html document and then save it as a .doc document.

        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value; // Values we don't care about
        object fileName = "C:/webpage.htm";
        object newFileName = "C:/webpage.doc";       

        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        // word.Visible = true; // To see what's happening

        Microsoft.Office.Interop.Word.Document document = 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);

        document.Activate();

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

        document.SaveAs(ref newFileName, ref saveFormat, 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, ref missing);

        document.Close(ref missing, ref missing, ref missing);

Note

  • You have to add a reference to Microsoft.Office.Interop.Word or something similar
  • The number of ref missing arguments depends on wich version of Word you are using
  • You have to use full paths in the filename as the Word instance starts from the System folder.
Bessi
A: 

Do remember that MSIE's (or Word's, for that matter -- AFAIK MS Office still uses its own) rendering engine isn't as reliable as you might want it to be, so anything beyond simple formatting might appear different in a Word document than in your browser.

Alsoplustoo, Word's interpretation of the DOC format might differ from your converter's -- OO.o had that problem for ages.

Alan
A: 

This is a .Net library to convert HTML to Word in C#, it works without using MS Office.

For example, you want to convert HTML string to Word file:

SautinSoft.HtmlToRtf.Converter h = new SautinSoft.HtmlToRtf.Converter();
h.ConvertStringToFile(htmlString, @"c:\Test.doc");
Maximus
A: 

Is there any way i can convert msoffice files to html format using such similar techniques

K.P.Raja Sekhar