Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. 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.
Written by Allen Wyatt (last updated December 9, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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 2016" 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 2016". If you are using a different version of Word, you should modify this line so it refers to your version, such as "Word 2019".
Note:
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, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Auto-incrementing Form Fields.
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!
After you have created your custom form, you will need to save it so that you can use it as often as needed. Word makes ...
Discover MoreWord allows you to create forms and then export the data from those forms into files that can be used by other programs. ...
Discover MoreIf 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 ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-12-09 17:21:43
Margaret Tassin
I have had to do this before but in a distributed environment with multiple copies of the template, so all users had to create their own unique numbers. We used a concatenated short date and time (total 12 characters) and appended the form-filler's initials to create a unique number. This number was not in sequence but it was unique.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments