Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Auto-incrementing Form Fields.

Auto-incrementing Form Fields

by Allen Wyatt
(last updated October 3, 2016)

4

When you are working with forms, you save the forms as templates that are used as the basis of new documents in Word. There may be times when you want to add a form field that increments every time you create a new document based on your form template. For instance, you might have a sequence number that you need for a data input form, or you may want to use a form field to indicate an invoice number.

Word does not have any fancy auto-increment fields. However, by combining some of the tools already available in Word, you can create a template and form field to do the trick for you. Start with this macro:

Sub AutoNew()
   ' Exit macro if no fields in document
   If ActiveDocument.FormFields.Count = 0 Then Exit Sub
   ' Create variables.
   Dim OfficeAppName As String
   Dim RegSection As String
   Dim RegKey As String
   Dim FieldObj As Object
   Dim RegValue As Long

   OfficeAppName = "Word 2007"
   RegSection = "UserData"
   RegKey = "Current Counter"
   ' Generate error if form field doesn't exist
   On Error GoTo ErrHandler
   Set FieldObj = ActiveDocument.FormFields("IncField")
   ' Get stored registry value, if any
   RegValue = GetSetting(OfficeAppName, RegSection, RegKey, 0)
   ' If not previously set, set to default
   If RegValue = 0 Then RegValue = 1
   ' Set form field result to stored value
   FieldObj.Result = CStr(RegValue)
   ' Increment and update invoice number
   SaveSetting OfficeAppName, RegSection, RegKey, RegValue + 1
ErrHandler:
   If Err <> 0 Then
      MsgBox Err.Description
   End If
End Sub

This macro should be saved only in the template that you will use to create your forms. The macro must be named AutoNew, so it will run whenever a document is created that is based on the form template. You should also make sure that you have a text form field in your document named IncField, which will display the incremental number. The number is tracked in the Registry so it can be accessed for future forms.

Notice the line in the macro that sets the OfficeAppName variable. In this version of the macro, it is set to the phrase "Word 2007". If you are using a different version of Word, you should modify this line so it refers to your version, such as "Word 2010".

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11431) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Auto-incrementing Form Fields.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Formatting Fractions

Need to have a great looking fraction in a document? It's relatively easy to do if you apply the formatting techniques ...

Discover More

Getting Rid of Stubborn Icons

Ever tried to clean-up the icons on your toolbar only to have one that won't go away? This tip explores some possible ...

Discover More

Using Overtype Mode

When you type information into a document, what you type normally is inserted just the left of the insertion point. Word ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Saving Form Data for a Database

Use Word to create a form, and you can easily collect standardized data from a large number of users. When it comes time to ...

Discover More

Text Form Fields in Word 2007

Word allows you to create fill-in-the-blank forms that are great for collecting information from users. A staple of such ...

Discover More

Working with Form Fields

You know you want to use form fields in your document (they are essential in creating forms, after all) but you need to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 + 0?

2016-07-21 13:40:53

Eleanor Flynn

I have been trying to set up a court transcript page with Questions and Answers; each question being a consecutive number. Is there any way in Word to do that?


2014-01-31 12:23:34

John McMullen

An additional problem with using the registry is that a large corporation might set the policies to restrict the registry. That's true where I work, for instance: the IT department keeps a short (sometimes too short!) leash on adding or changing the registry.


2013-05-08 11:29:36

Barry Fitzpatrick

Storing the value in the registry has got its limitations/issues:

1. the registry value is only accessible on a single machine, so sharing the file is not feasible (unless duplicate values are acceptable).

2. the value could change if say a "System Restore" to an earlier time is actioned or if the OS software is re-installed.

3. The value is lost if the files are transferred to another machine, e.g. say, if the User upgrades to a new machine.

4. the stored value may not be backed up, if the back-up only archives data and not the registry.

5. Opening an existing document will increment the stored number.

An alternative is to use a "Custom Document property" which lives with the document. (This tip "http://wordribbon.tips.net/T012599_Creating_Custom_Document_Properties.html" describes how to create one), just store it in the FormField itself.

On opening the template/master document runs the "AutoNew" macro which increments the stored value and saves it back to the template/master file. Next it saves itself to some randomly named non macro-enabled file (effectively deleting the macro & releasing the template file for use by someone else in a shared environment)pending the User giving it a final name. If there are other macros that need to be preserved then another Custome property would need to be set to indicate the AutoNew routine needs to exit without incrementing the stored value or changing the filename.

As mentioned above as this is a macro the file needs to be stored as a macro enabled file. MS-Word security settings have to be set to allow the macro to run.


2013-02-07 18:20:43

JamesM

After I spend many hours figuring out that Mr What was making the assumption we all know as much as him, and figure that there are missing steps for Word 2007. This works beautifully.

The missing steps I found, are that you must create a "Macro Enabled" Template for this code to work and "Form Fields" are the LEGACY form fields, not the 2007 - 2010 new Text fields.

I hope this helps others.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.