
Tips.Net > WordTips Home > Fields > Auto-incrementing Form Fields
Summary: Templates are a great way to create new documents because they act as intricate patterns to what those new documents should contain. What if you want the new documents to include some sort of automatically incrementing number? This tip looks at ways you can accomplish the task. (This tip works with Microsoft Word 97, Word 2000, Word 2002, Word 2003, and Word 2007.)
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 VBA 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 2002"
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 2002". If you are using a different version of Word, you should modify this line so it refers to your version, such as "Word 2000" or "Word 2007".
Tip #1329 applies to Microsoft Word versions: 97 2000 2002 2003 2007
Find and Replace Almost Anything! Learn the ins and outs of the powerful search engines available in Word. Learn to search for and replace text, formatting, graphics, and special characters the easy way with WordTips: Find and Replace.
Discover how to use Word's mail merge tool to create your own custom documents in just minutes. Great e-book answers all your questions. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips