Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Read Microsoft Word Document File by using C#
Read Microsoft Word Document File by using C#
Read Microsoft Word Document File by using C#


by Arun Singh on 8/29/2011 9:45:20 PM

Views: 31517       Comments: 3

Read Microsoft Word Document File by using C#

It is an efficient way through which we can read Microsoft Word Document file (or .docx extension file) by using C#.Net code.

Here I am making a window form application to read text of MS-Word file (Not image) and showing the ms-word text into windows form richtextbox control. Let’s see the brief demonstration of reading text from ms-word file in c#.

Step 1:  To read MS-Word file write the following code on Read Button click event.

/// <summary>

///This method read the document and writes into richtextbox control 

/// </summary>

 

        private void btn_Read_Click(object sender, EventArgs e)

        {

            // call the method to read ms-word document

            ReadMsWord();

        }

/// <summary>

        /// Read ms-word file

        /// </summary>

        public void ReadMsWord()

        {  

            // variable to store file path

            string filePath = null;

            // open dialog box to select file

            OpenFileDialog file = new OpenFileDialog();

            // dilog box title name

            file.Title = "Word File";

            // set initial directory of computer system

            file.InitialDirectory = "c:\\";

            // set restore directory

            file.RestoreDirectory = true;

 

            // execute if block when dialog result box click ok button

            if (file.ShowDialog() == DialogResult.OK)

            {

                // store selected file path

                filePath = file.FileName.ToString();          

            }

 

            try

            { 

                // create word application

                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();

                // create object of missing value

                object miss = System.Reflection.Missing.Value;

                // create object of selected file path

                object path = filePath;

                // set file path mode

                object readOnly = false;

                // open document                

                Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,                                                                                    ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

              // select whole data from active window document

                docs.ActiveWindow.Selection.WholeStory();

                // handover the data to cllipboard

                docs.ActiveWindow.Selection.Copy();

                // clipboard create reference of idataobject interface which transfer the data

                IDataObject data = Clipboard.GetDataObject();

                //set data into richtextbox control in text format

                richTextBox2.Text = data.GetData(DataFormats.Text).ToString();

                // read bitmap image from clipboard with help of iddataobject interface

                Image img = (Image)data.GetData(DataFormats.Bitmap);

                // close the document

                docs.Close(ref miss, ref miss, ref miss);

            }

            catch (Exception ex) { MessageBox.Show(ex.ToString()); }

           

        }

Step 2: After write the above code debug the program and click on Read button

Read Microsoft Word Document File by using C#

When you click on Read button then a file dialog box will be appear for select an ms-word file, after select a file then press Open button.

Read Microsoft Word Document File by using C#

After clicked open button the following output will be appear as follows.

Read Microsoft Word Document File by using C#

Report Abuse Form
Reason:    
 


Read Microsof Word Documents
by Rohit Kesharwani 9/20/2011 3:07:52 AM
Nice article. It will help a lot.
Report Abuse

Re: Read Microsof Word Documents
by Arun Singh 9/24/2011 12:52:30 AM
Thanks Rohit !!!!!
Report Abuse

Alternative to read Word files in C#
by James Howard 3/1/2012 4:34:27 AM
Hi,

nice article!

For ones that are unable to use .NET Word automation, try this C# / VB.NET Word library that doesn't require Word application.

Here is a sample C# read Word:
// Load a document.
DocumentModel document = DocumentModel.Load("Document.docx", DocxLoadOptions.DocxDefault);

// Iterate over all paragraphs in the document.
foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph))
{
    // Iterate over all runs in the paragraph and write their text to console.
    foreach (Run run in paragraph.GetChildElements(true, ElementType.Run))
        Console.Write(run.Text);
    Console.WriteLine();
}
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3430
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2014MindStick. All Rights Reserved.