Saving Word document into web page using C#
The below code snippet will convert a wrd document into a .htm web page. This function is ready to use and can be called wherever needed.
private void DocToWebpage()
{
//Creating the instance of Word Application
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
// specify the Source & Target file names
object Source = "D:\\Sample.docx";
object Target = "D:\\Sandbox.htm";
// Use for the parameter whose type are not known or
// say Missing
object Unknown =Type.Missing;
// Source document open here
// Additional Parameters are not known so that are
// set as a missing type
newApp.Documents.Open(ref Source,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,ref Unknown, ref Unknown);
// Specifying the format in which you want the output file
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target,ref format,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown);
// for closing the application
newApp.Quit(ref Unknown,ref Unknown,ref Unknown);
}
Note: The above snippet has been implemented over MS Office 2007. The source document can be .docx or .doc. The target format can be any format supported by MS Word.
To invoke this method, these references must be added.
using Microsoft.Office;
using Microsoft.Office.Interop;
The assemblies to be refered are:
<add assembly="Microsoft.Office.Tools.Word, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.Office.Interop.Word, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> <add assembly="Office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>