Document Editing Problems

How to Automate a Rolling Six-Digit Number in Microsoft Word

Phi Hung VoPhi Hung Vo Oct 9, 2026 870 views

Question details

The user needs a way to automatically increment a six-digit sequential number each time a Microsoft Word document is saved.

How to Automate a Rolling Six-Digit Number in Microsoft Word
Product
Microsoft Word
Device & OS
not provided
Scenario
Creating reusable templates for invoices, receipts, or purchase orders where each newly saved document requires a unique, rolling serial number.
Observed behavior
Microsoft Word lacks a native feature to automatically increment numbers when using the Save As function, requiring the use of custom fields and macros.
Before you start

Ensure you are comfortable working with VBA Editor. You will need to enable macros in your Trust Center settings and save your template as a Macro-Enabled Document (.docm) to ensure the rolling number script executes automatically.

Solution 1Recommended

Use a DOCVARIABLE Field and a VBA Macro

Since Word has no native auto-increment feature, you must combine a DOCVARIABLE field formatted with leading zeros and a customized VBA macro to read, increment, and rewrite the counter upon saving.

By storing the sequence number inside a document variable, you can use Visual Basic for Applications (VBA) to intercept the save action. The macro will automatically add 1 to the current variable value and refresh the field display to show the new six-digit number.

1
Insert the DOCVARIABLE Field

Place your cursor where you want the number to appear (such as the document header). Press Ctrl+F9 to insert empty field braces, and type DOCVARIABLE Counter \# "000000" inside them to force the six-digit format.

2
Open the VBA Editor

Press Alt+F11 to open the Visual Basic for Applications editor. Right-click on 'ThisDocument' in the Project Explorer panel, select Insert, and then click Module.

3
Write the Increment Macro

In the new module, write a script that accesses ActiveDocument.Variables("Counter").Value, adds 1 to it, and runs ActiveDocument.Fields.Update to refresh the display. You can assign this script to intercept the 'FileSaveAs' command.

4
Save as a Macro-Enabled Document

Go to File > Save As. In the 'Save as type' dropdown, you must select 'Word Macro-Enabled Document (*.docm)' so that your automation script is preserved for future use.

Use a DOCVARIABLE Field and a VBA Macro
Macro Requirements: The document cannot be macro-free or fully read-only while the counter is being updated. The auto-increment functionality completely relies on the macro executing properly.
WPS Office Writer

Automate Document Numbering Easily with WPS Writer

WPS Office Writer fully supports VBA macros and DOCVARIABLE fields natively, making it incredibly easy to set up automated document numbering templates just like you would in Microsoft Word.

  1. 1. Access Developer Tools: Open WPS Writer, navigate to the Developer tab on the top ribbon, and click on VBA Editor.
  2. 2. Insert Document Variable: Press Ctrl+F9 in your document and type DOCVARIABLE Counter \# "000000" to define your six-digit format.
  3. 3. Implement the Macro: Use the VBA Editor in WPS to write your sequence incrementing script just as you would using standard VBA syntax.
  4. 4. Save Your Template: Click Menu > Save As, and choose the Macro-Enabled Document (.docm) format to ensure your automated numbering works on every save.
Create and run VBA macros natively with built-in Developer toolsHighly compatible with Microsoft Office formats, including .docm filesLightweight, fast, and completely free to useFamiliar ribbon interface for seamless migration without a learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Can I automate a rolling number without using VBA macros?

No. Microsoft Word does not offer a built-in feature to auto-increment values automatically upon saving or printing. A macro is strictly required to change the variable's value dynamically.

Why does my number format drop the leading zeros?

If your number displays as '1' instead of '000001', you need to ensure the numeric picture switch (\# "000000") is properly added to your DOCVARIABLE field code. Right-click the field, select 'Toggle Field Codes', add the switch, and press F9 to update.

Will the auto-numbering macro work if I save the file as a PDF?

The macro typically triggers on the native save event. When exporting or saving as a PDF, the document prints the current number on the screen. However, you must still save the source Word document to successfully increment the counter for the next session.