Tuesday, September 17, 2013

Reading paragraph from a MS Word document using C#


Here I am going to share how we can read paragraphs from a Microsoft Word Documnet using Microsoft.Office.Interop.Word DLL.

(1) Add reference of Microsoft.Office.Interop.Word DLL into your project
(2) With the help of following class, we can read a specific paragraph of a MS Word Document.


   class WordDocReader
    {
        //To read paragraph contents of a Word document using Microsoft.Office.Interop.Word DLL
        public  string ReadFileContent(string path, int paraGraphNum)
        {
            int i = 0;
 
            StringBuilder sb = new StringBuilder();
 
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
 
            object file = path;
 
            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);
 
 
            Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;
 
            // Count number of paragraphs in the file
            long parCount = DocPar.Count;
 
            // Step through the paragraphs
            while (i < parCount)
            {
                i++;
                if (i == paraGraphNum)
                {
                    sb.Append(DocPar[i].Range.Text);
                    break;
                }
            }
 
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return(sb.ToString());
        }
    }

No comments:

Post a Comment