Read Microsoft Word File using C# program

Use this program to read the Microsoft Word file.

Complete C# Code:
---------------------

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using System.Reflection;

namespace ReadWordFile
{
    class Program
    {

        [STAThread]
        static void Main()
        {

            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object file = "C:\\local\\mywordfile.doc"; // Specify path for word file
            object nullobj = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                                                                ref nullobj, ref nullobj, ref nullobj,
                                                                                ref nullobj, ref nullobj, ref nullobj,
                                                                                ref nullobj, ref nullobj, ref nullobj,
                                                                                ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.ActiveWindow.Selection.WholeStory();
            doc.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();
            string allText = data.GetData(DataFormats.Text).ToString();
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            Console.WriteLine(allText);

        }
    }
}
By Perry    Popularity  (2587 Views)