.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Convert Word to other formats

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

This code snippet shows how to convert a Microsoft Word document to other document formats such HTML, Rich Text Format (RTF), Text, or XML.
 


This code snippet shows how to convert a Microsoft Word document to other document formats such HTML, Rich Text Format (RTF), Text, or XML.

We are going to use Office Interop for this purpose. Before you can use the code, you must add a reference to Office Interop Work library. Follow these steps.

  • Right click on your project.
  • Select Add Reference from menu items.
  • Go to COM tab
  • Select Microsoft.Office.Interop.Word

Now you are all set. Copy this below method and call this method within your application and pass original file name, file type and other information.

public void WordFileTo(string originalFile, object SavePath, string SaveFile, string FileTo)

{

  try

  {

    if (SaveFile == "")

    {

         return;

    }

 

    if (originalFile.Trim() != "" && SavePath.ToString().Trim() != "")

    {

      Microsoft.Office.Interop.Word.ApplicationClass WordApp = new

            Microsoft.Office.Interop.Word.ApplicationClass();

      Microsoft.Office.Interop.Word.Document WordDoc = new

Microsoft.Office.Interop.Word.Document();

      object DocNoParam = Type.Missing;

      object DocFileName = originalFile;

      object DocReadOnly = false;

      object DocVisible = false;

      object SaveToFormat = "";

 

      // Open the document by passing the path

      WordDoc = WordApp.Documents.Open(ref DocFileName, ref DocNoParam, ref DocReadOnly,

ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam,

ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocVisible,

ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam,

ref DocNoParam);

      WordDoc.Activate();

 

      if (FileTo == "html")

      {

         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

         SavePath = SavePath + SaveFile + ".html";

      }

      if (FileTo == "rtf")

      {

         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;

         SavePath = SavePath + SaveFile + ".rtf";

      }

      if (FileTo == "txt")

      {

         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

         SavePath = SavePath + SaveFile + ".txt";

              }

      if (FileTo == "xml")

      {

         SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;

         SavePath = SavePath + SaveFile + ".xml";

      }

     

//Save the document to the specified path and format

      WordDoc.SaveAs(ref SavePath, ref SaveToFormat, ref DocNoParam, ref DocNoParam, ref

                  DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref

DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam, ref DocNoParam);

 

 

      // Since we don't want to save changes to the original document,

Close the document, save no changes

     object SaveChanges = false;

      WordApp.Quit(ref SaveChanges, ref DocNoParam, ref DocNoParam);

    }

    else

    { return; }

  }

  catch (Exception)

  {

      return;

  }

}

 

If you have any questions, post them below.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend