logo
search
VBA & Macro Problems

How to Extract Word Comments with Continuous Line Numbers via VBA

Khadija KhanKhadija Khan Oct 22, 2026 869 views

Question details

The user needs to extract comments from a Word document and reference them with cumulative, continuous line numbers using VBA.

How to Extract Word Comments with Continuous Line Numbers via 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Word to launch the Visual Basic for Applications (VBA) editor, and insert a new Module.

2
Set Up the Loop and Range

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'.

3
Compute Exact Preceding Lines

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.

4
Extract and Output

Store or print the comment's author, text, and the calculated continuous line number to a new document, array, or text file.

Calculate Cumulative Lines Using Document Range Statistics
Accuracy Maintained: Using the ComputeStatistics method on a dynamic range guarantees an accurate line count even if your document contains complex layouts, tables, or inserted images.
Free Microsoft Office alternative

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. 1. Download and Install: Visit the official WPS Office website to download the free suite and install it on your device.
  2. 2. Open Your Document: Launch WPS Writer and open your existing DOCX or DOCM file directly without losing any formatting.
  3. 3. Manage Comments Easily: Navigate to the Review tab to view, extract, and manage document comments through a highly intuitive sidebar.
Seamless compatibility with Microsoft Office formats (DOCX, DOC, DOCM)Lightweight installation and incredibly fast load timesFamiliar user interface requiring zero learning curveBuilt-in robust tools for efficient document review and comment management
microsoft office alternative - wps office

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.