Wednesday, April 11, 2012

Convert a Docx file to HTML


//Add Reference
using System.Reflection; // Microsoft.Office.Interop.Word.dll

//Source File
 string strSource=@"D:\Documentfile.docx";
// Destination  File
 string strDestination=@"D:\htmlfile.html";

// Constant for WORD-TO-HTML exporting format
const int WORD_HTML_FORMAT = 8;

// Load COM-Metadata of Word application from registry
Type tWordApplication = Type.GetTypeFromProgID("Word.Application");

// Create new instance of Word
object objWord = Activator.CreateInstance(tWordApplication);

// List all documents
object objDocuments = tWordApplication.InvokeMember("Documents", BindingFlags.IgnoreCase | BindingFlags.GetProperty | BindingFlags.Public, null, objWord, new object[0]);

// Get COM-Metadata of Word Documents
Type tWordDocuments = objDocuments.GetType();

// Load source
object objDocument = tWordDocuments.InvokeMember("Open", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objDocuments, new object[1] { strSource });

// Get COM-Metadata of Word Documents
Type tWordDocument = objDocument.GetType();

// Create HTML
tWordDocument.InvokeMember("SaveAs", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objDocument, new object[2] { strDestination, WORD_HTML_FORMAT });

// Close Word
tWordApplication.InvokeMember("Quit", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objWord, new object[0]);

No comments:

Post a Comment

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...