Thursday, November 18, 2010

Word2CHM, convert a word document to a CHM file.

Word2CHM is a open source C# program which can convert MS Word document(in 2000/2003 format) to a CHM document. It require HTML Help Workshop and MS Word 2003.
Background
Many people write customer help document with MS Word, because MS Word is very fit to write document include text, images and tables.
But many customers did not want read help document in MS Word format, but they like CHM format. So it is useful than convert ms word document to CHM document. This is why I build Word2CHM.
Word2CHM
In Word2CHM , there are three steps in converting ms word document to CHM document . First is convert ms word document to a single html file, second is split a single html file to multi html files, and thirst is compile multi html files to a single CHM file.
First, Convert ms word document to a single html file
MS Word application support OLE automatic technology, a C# program can host a ms word application, open ms word binary document and save as a html file.
There are some sample C# code that hosts a ms word application.
private bool SaveWordToHtml(string docFileName, string htmlFileName)
{
   // check doc file name
   if (System.IO.File.Exists(docFileName) == false )
   {
       this.Alert("File '" + docFileName + "' not exist!");
       return false;
   }
   // check output directory
   string dir = System.IO.Path.GetDirectoryName(htmlFileName);
   if (System.IO.Directory.Exists(dir) == false )
   {
       this.Alert("Directory '" + dir + "' not exist!");
       return false;
   }
   object trueValue = true;
   object falseValue = false;
   object missValue = System.Reflection.Missing.Value;
   object fileNameValue = docFileName;
   // create word application instance
   Microsoft.Office.Interop.Word.Application app =
       new Microsoft.Office.Interop.Word.ApplicationClass();
   // set word application visible
   // if something is error and quit , user can close word application by self.
   app.Visible = true;
   // open document
   Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
       ref fileNameValue,
       ref missValue,
       ref trueValue,
       ref missValue,


Read more: Codeproject