VSTO 2007: how do I determine the page and paragraph number of a Range?

calendar_today Asked May 14, 2009
thumb_up 9 upvotes
history Updated April 16, 2026

Question posted 2009 · +2 upvotes

I’m building an MS Word add-in that has to gather all comment balloons from a document and summarize them in a list. My result will be a list of ReviewItem classes containing the Comment itself, the paragraph number and the page number on which the commented text resides.

Part of my code looks like this:

    private static List<ReviewItem> FindComments()
    {
        List<ReviewItem> result = new List<ReviewItem>();
        foreach (Comment c in WorkingDoc.Comments)
        {
            ReviewItem item = new ReviewItem()
            {
                Remark = c.Reference.Text,
                Paragraph = c.Scope. ???, // How to determine the paragraph number?
                Page = c.Scope. ??? // How to determine the page number?
            };
            result.Add(item);
        }
        return result;
   }

The Scope property of the Comment class points to the actual text in the document the comment is about and is of type Microsoft.Office.Interop.Word.Range. I can’t work out how to determine what page and which paragraph that range is located.

With paragraph number, I actually mean the “numbered list” number of the paragraph, such as “2.3” or “1.3.2”.

Any suggestions? Thanks!

Accepted answer +9 upvotes

Try this for page number:

Page = c.Scope.Information(wdActiveEndPageNumber);

Which should give you a page number for the end value of the range. If you want the page value for the beginning, try this first:

Word.Range rng = c.Scope.Collapse(wdCollapseStart);
Page = rng.Information(wdActiveEndPageNumber);

For paragraph number, see what you can get from this:

c.Scope.Paragraphs; //Returns a paragraphs collection

My guess is to take the first paragraph object in the collection the above returns, get a new range from the end of that paragraph to the beginning of the document and grab the integer value of this:

[range].Paragraphs.Count; //Returns int

This should give the accurate paragraph number of the beginning of the comment range.

4 code variants in this answer

  • Variant 1 — 1 lines, starts with Page = c.Scope.Information(wdActiveEndPageNumber);
  • Variant 2 — 2 lines, starts with Word.Range rng = c.Scope.Collapse(wdCollapseStart);
  • Variant 3 — 1 lines, starts with c.Scope.Paragraphs; //Returns a paragraphs collection
  • Variant 4 — 1 lines, starts with [range].Paragraphs.Count; //Returns int

Word VBA objects referenced (5)

  • Interop.Word — Exporting a Range to a Table in a Word Document
  • Interop.Word — Using events with the Application object (Word)
  • Microsoft.Office — Controlling One Microsoft Office Application from Another
  • Microsoft.Office — List the Name and Office Location of Each Manager Belonging to an Exchange Distribution List
  • Paragraphs.Count — Count function (Microsoft Access SQL)

Top ms-word Q&A (6)

+9 upvotes ranks this answer #11 out of 31 ms-word solutions on this site .