Tuesday, December 23, 2014

How to extract text from MS word in C#

11:54 PM - By azeheruddin khan 0


In this sample code, I'll demonstrate a simple way to obtain document text from *.doc files.


using System;
using System.Windows.Forms;
//this namespace consists of classes,methods and interfaces used for interacting with word documents.
using Microsoft.Office.Interop.Word;

namespace WordtoWindowApp
{
    public partial class WindowWord : Form
    {
         public WindowWord()
        {
            InitializeComponent();
        }

         private void show_file_Click(object sender, EventArgs e)
         {
             try
             {
                 /*we are creating an object of Application interface in order to create an instance of a word 
                   application so that we can work with the word environment.*/
                 Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();

                 object file = filepath.Text;//file path

                 object nullobject = System.Reflection.Missing.Value;

                 /* document is an interface which represents a word document, to open an existing document or 
                  to create a new document we need to create an object for this interface.*/
                 Document docs = wordobject.Documents.Open(ref file, ref nullobject, ref nullobject, ref nullobject, 
                     ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, 
                     ref nullobject, ref nullobject, ref nullobject, ref nullobject);

                 //the document which has focus is represented by the property called ActiveWindow.
                 docs.ActiveWindow.Selection.WholeStory();

                 //selection is a property used for selecting content of a word document.
                 docs.ActiveWindow.Selection.Copy();

                 /*IDataObject is an interface of System.Windows.Forms namespace which provides a format independent  mechanism of 
                   transferring data.*/

                 /*Clipboard is a class which provides methods for placing data on or retrieving data from clipboard. 
                   this Clipboard acts like a buffer between selected word document and the textbox into which the 
                   contents of word document are to be copied.*/

                 //GetDataObject method retrieves data that is currently on system Clipboard.
                 
                 IDataObject data = Clipboard.GetDataObject();
                 wordcontent.Text = data.GetData(DataFormats.Text).ToString();
                 docs.Close(ref nullobject, ref nullobject, ref nullobject);
             }
             catch (Exception j)
             {
                 MessageBox.Show(j.Message);
             }

         }

         private void sel_file_Click(object sender, EventArgs e)
         {
             try
             {
                 //class present in System.Windows.Forms namespace used for prompting the user to open a file. not inheritable
                 OpenFileDialog path = new OpenFileDialog();
                 //path.Title = "ajay files";
                 path.InitialDirectory = @"e:\";

                 if (path.ShowDialog() == DialogResult.OK)
                 {
                   filepath.Text = path.FileName.ToString();
                 }
             }
             catch (Exception l)
             {
                 MessageBox.Show(l.Message);
             }

         }
    }
}


Output:




You can download all the sample code from below link.




About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top