Search code examples
c#ms-worddocx

How to get a paragraphs number in a word document with C#?


I use the assembly Microsoft.Office.Interop.Word;

My document is a Microsoft.Office.Interop.Word.Document object, and I want to get the number of each paragraph in this document.

How can I do this?


Solution

  • You need something like this:

        object misValue = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        object docPth = @"c:\tmp\aDoc.doc";
        Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref docPth, ref misValue, ref misValue,
            ref misValue, ref misValue, ref misValue, ref misValue, ref misValue, ref misValue, ref misValue,
            ref misValue, ref misValue, ref misValue, ref misValue, ref misValue, ref misValue);
        wordApp.Visible = true;
        foreach (Microsoft.Office.Interop.Word.Paragraph aPar in aDoc.Paragraphs)
        {
            Microsoft.Office.Interop.Word.Range parRng = aPar.Range;
            string sText = parRng.Text;
            string sList = parRng.ListFormat.ListString;
            int nLevel = parRng.ListFormat.ListLevelNumber;
            MessageBox.Show("Text = " + sText + " - List = " + sList + " - Level " + nLevel.ToString());
        }