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

Activating the Formula Bar with the Keyboard

Hate to take your hands off the keyboard while working on a worksheet? Here's one way to activate the Formula Bar without the ...

Discover More

Detecting Hidden Rows

Excel allows you to easily hide rows in a worksheet, so their contents are not visible. Figuring out how to detect where ...

Discover More

Boxes in Boxes

When you insert a text box within another text box, you may expect any text in the outer text box to wrap around the inner ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Stopping Enter from being Pressed In a Form

If you create a form using Word, chances are good that you don't want a user to mess up the layout of the form by pressing ...

Discover More

Handling Returns in Form Data

Word allows you to create forms and then export the data from those forms into files that can be used by other programs. Part ...

Discover More

Valid Numbers in Form Fields

When you create a form you need to use special form fields. If you want to limit what users can enter in a form field, you'll ...

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 6Mpixels. 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 7 - 1?

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.