Word to HTML Converter using ASP.Net 2.0 and Microsoft 11.0 object Library

Using Microsoft object library you can do many functionalities like creating new document, appending existing document, counting no of lines, size of the document, Spell Check etc....

Required:

This application require to have COM reference to the project, which can be done as:

Right clicking in the solution explorer on References->>Add Reference.

Click on the COM tab and look for the Microsoft Word 11.0 Object Library. Click Select and OK.

You can use the following code in your .cs page as:
  1. using Microsoft.Office;  
  2. using Microsoft.Office.Interop.Word;   
In this project I am uploading file with only extension .DOC to a folder called as "Uploaded" and converting to HTML document and storing the HTML in "WordToHtml" folder.

Some of the declarations required in the project are as follows:

  1. protected Microsoft.Office.Interop.Word.ApplicationClass objWord = new ApplicationClass();  
  2. //This creates new object of Word.ApplicationClass  
  3. protected string strPathToUpload;  
  4. //Path to upload files "Uploaded"  
  5. protected string strPathToConvert;  
  6. //Path to convert uploaded files and save  
  7. object fltDocFormat = 10;  
  8. //For filtered HTML Output  
  9. protected object missing = System.Reflection.Missing.Value;  
  10. //Is just to skeep the parameters which are passed as boject reference, these are seems to be optional parameters  
  11. protected object readOnly = false;  
  12. protected object isVisible = false;  
  13. //The process has to be in invisible mode   
On a aspx page I am having only two controls as:
  1. <asp:FileUpload ID="fUpload" runat="server"/> &   
  2. <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />  
On button click executes following functionality:
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2. {  
  3.     //Code to check if user has selected any file on the form  
  4.     if (!(fUpload.HasFile))  
  5.     {  
  6.         lblMessage.Text = "Please choose file to upload";  
  7.     }  
  8.     else  
  9.     {  
  10.         try  
  11.         {  
  12.             //To check the file extension if it is word document or something else  
  13.             string strFileName = fUpload.FileName;  
  14.             string[] strSep = fUpload.FileName.Split('.');  
  15.             int arrLength = strSep.Length - 1;  
  16.             string strExt = strSep[arrLength].ToString().ToUpper();  
  17.             //Save the uploaded file to the folder  
  18.             strPathToUpload = Server.MapPath("Uploaded");  
  19.             //Map-path to the folder where html to be saved  
  20.             strPathToConvert = Server.MapPath("WordToHtml");  
  21.             object FileName = strPathToUpload + "\\" + fUpload.FileName;  
  22.             object FileToSave = strPathToConvert + "\\" + fUpload.FileName + ".htm";  
  23.             if (strExt.ToUpper().Equals("DOC"))  
  24.             {  
  25.                 fUpload.SaveAs(strPathToUpload + "\\" + fUpload.FileName);  
  26.                 lblMessage.Text = "File uploaded successfully";  
  27.                 //open the file internally in word. In the method all the parameters should be passed by object reference  
  28.                 objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,  
  29.                 ref missing, ref  missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,  
  30.                 ref missing, ref missing);  
  31.                 //Do the background activity  
  32.                 objWord.Visible = false;  
  33.                 Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;  
  34.                 oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing,  
  35.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
  36.                 ref missing, ref missing);  
  37.                 lblMessage.Text = fUpload.FileName + " converted to HTML successfully";  
  38.             }  
  39.             else  
  40.             {  
  41.                 lblMessage.Text = "Invalid file selected!";  
  42.             }  
  43.             //Close/quit word  
  44.             objWord.Quit(ref missing, ref missing, ref missing);  
  45.         }  
  46.         catch (Exception ex)  
  47.         {  
  48.             Response.Write(ex.Message);  
  49.         }  
  50.     }  
  51. }  
Note: The constant object fltDocFormat = 10; is a unique number that specifies an external file converter. Setting it to 10 creates Filtered HTML files. For regular HTML output use the number 8. This will produce bigger HTML files but will maintain the Word formatting.
 
On some of the machines you need to have "IUSER_MACHINE" rights to upload files to particular folders.


Similar Articles