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:
- using Microsoft.Office;
- 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:
- protected Microsoft.Office.Interop.Word.ApplicationClass objWord = new ApplicationClass();
-
- protected string strPathToUpload;
-
- protected string strPathToConvert;
-
- object fltDocFormat = 10;
-
- protected object missing = System.Reflection.Missing.Value;
-
- protected object readOnly = false;
- protected object isVisible = false;
-
On a aspx page I am having only two controls as:
- <asp:FileUpload ID="fUpload" runat="server"/> &
- <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
On button click executes following functionality:
- protected void btnUpload_Click(object sender, EventArgs e)
- {
-
- if (!(fUpload.HasFile))
- {
- lblMessage.Text = "Please choose file to upload";
- }
- else
- {
- try
- {
-
- string strFileName = fUpload.FileName;
- string[] strSep = fUpload.FileName.Split('.');
- int arrLength = strSep.Length - 1;
- string strExt = strSep[arrLength].ToString().ToUpper();
-
- strPathToUpload = Server.MapPath("Uploaded");
-
- strPathToConvert = Server.MapPath("WordToHtml");
- object FileName = strPathToUpload + "\\" + fUpload.FileName;
- object FileToSave = strPathToConvert + "\\" + fUpload.FileName + ".htm";
- if (strExt.ToUpper().Equals("DOC"))
- {
- fUpload.SaveAs(strPathToUpload + "\\" + fUpload.FileName);
- lblMessage.Text = "File uploaded successfully";
-
- objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
- ref missing, ref missing);
-
- objWord.Visible = false;
- Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
- oDoc.SaveAs(ref FileToSave, ref fltDocFormat, 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);
- lblMessage.Text = fUpload.FileName + " converted to HTML successfully";
- }
- else
- {
- lblMessage.Text = "Invalid file selected!";
- }
-
- objWord.Quit(ref missing, ref missing, ref missing);
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- }
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.