How to Extract Word Comments with Continuous Line Numbers via VBA
Question details
The user needs to extract comments from a Word document and reference them with cumulative, continuous line numbers using VBA.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA script to export document comments along with accurate, document-wide line numbers for auditing or referencing purposes.
- Observed behavior
- The default VBA method returns line numbers that restart at the beginning of every page, failing to provide a continuous document-wide line count.
Ensure that your document's line numbering is visibly enabled via the Layout tab and set to 'Continuous' so you can verify the script's output against the document text.
Calculate Cumulative Lines Using Document Range Statistics
Use Word's range statistics to count the exact number of lines preceding a comment, bypassing the page-restart issue entirely.
A simple formula like multiplying lines per page by the page count is highly unreliable because tables, figures, and varying paragraph spacing change the number of lines per page.
Instead, the most robust method is to define a text range from the start of the document up to the comment's reference mark, and then compute the exact line statistics for that specific range.
Press Alt + F11 in Word to launch the Visual Basic for Applications (VBA) editor, and insert a new Module.
Write a 'For Each' loop to iterate through 'ActiveDocument.Comments'. For each comment, define a Range object starting at position 0 up to 'Comment.Reference.Start'.
Apply the 'ComputeStatistics(wdStatisticLines)' method to your newly defined Range. This returns the precise continuous line number up to the comment's location, ignoring page boundaries.
Store or print the comment's author, text, and the calculated continuous line number to a new document, array, or text file.

Track Line Counts Page by Page
Iterate through document pages sequentially to build a running total of lines per page before calculating the final comment line position.
Manage Word Documents and Formatting Seamlessly with WPS Office
If you are struggling with complex VBA line numbering discrepancies in Microsoft Word, try WPS Office. It provides an intuitive interface for managing comments, handling complex document layouts, and ensuring your text aligns perfectly, all while maintaining strict compatibility with traditional Word formats.
- 1. Download and Install: Visit the official WPS Office website to download the free suite and install it on your device.
- 2. Open Your Document: Launch WPS Writer and open your existing DOCX or DOCM file directly without losing any formatting.
- 3. Manage Comments Easily: Navigate to the Review tab to view, extract, and manage document comments through a highly intuitive sidebar.

Frequently Asked Questions
Why does Word VBA return restarting line numbers?
By default, the `wdFirstCharacterLineNumber` property in Word VBA is designed to return the line number relative to the current page. Unless you explicitly calculate a continuous count, the system resets this value back to 1 at the top of every new page.
How do tables and images affect line numbering in VBA?
Tables, figures, and varying paragraph spacing change the physical layout, meaning pages will not have a uniform number of lines. This makes simple mathematical formulas (like multiplying a fixed line count by the page number) highly inaccurate for calculating continuous totals.
Can I extract Word comments without using VBA scripts?
Yes, you can extract comments by navigating to the Review tab and using the 'Print' function to print only the 'List of Markup'. Alternatively, saving the document as an XML or Web Page (HTML) file allows you to extract comments using standard text parsers, though this will not capture visual line numbers.
Does WPS Office support VBA macros?
Yes, specific business and professional editions of WPS Office include full support for VBA macros, allowing you to run custom scripts, automate tasks, and manipulate document elements just as you would in Microsoft Office.




