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 DocumentInterop.Word— Using events with the Application object (Word)Microsoft.Office— Controlling One Microsoft Office Application from AnotherMicrosoft.Office— List the Name and Office Location of Each Manager Belonging to an Exchange Distribution ListParagraphs.Count— Count function (Microsoft Access SQL)
Top ms-word Q&A (6)
- XML – adding new line +19 (2012)
- How to open and manipulate Word document/template in Java? +18 (2012)
- Why does the file utility identify Microsoft Word files as CDF? What is this CDF? +15 (2011)
- Version Control for word documents +13 (2008)
- programatically convert word docx to doc without using ole automation +13 (2008)
- What makes Microsoft-Word-generated HTML documents so large in code? +12 (2015)
ms-word solutions on this site
.