Word

Autonumber a Document

Ease of Use

Easy

Version tested with

2000 

Submitted by:

Anne Troy

Description:

Sequentially number a Word file, such as an invoice or purchase order. 

Discussion:

You may want to automatically have each document of a specific type be numbered sequentially. You'll have to use a template for this to work correctly, since it requires a bookmark, and the bookmark gets replaced by the code. When you place this code in a template, each document created from it uses the code, but does not CONTAIN the code, so you can easily send the documents created from this template to outside parties. You must create 2 files--a text file and a template. Place the text file on a network server if more than one person will be using the template. 

Code:

instructions for use

			

Private Sub Document_New() ' 'Note the location where you create your text file 'must be changed in two places in this code - the following line and... oNum = System.PrivateProfileString("C:\increment.txt", "InvNmbr", "oNum") If oNum = "" Then oNum = 1 Else oNum = oNum + 1 End If 'you must also change the text file path here (next line) as well System.PrivateProfileString("C:\increment.txt", "InvNmbr", "oNum") = oNum ActiveDocument.Bookmarks("oNum").Range.InsertBefore Format(oNum, "00#") ActiveDocument.SaveAs FileName:="path" & Format(oNum, "00#") End Sub

How to use:

  1. Copy the code above.
  2. Open the document in which you want to place the numbers.
  3. Insert a bookmark called oNum in the location where you want the number inserted.
  4. Hit Alt+F11 to open the Visual Basic Editor (VBE).
  5. Double click ThisDocument and Paste the code into the code window at right.
  6. Change the path to the text file you created.
  7. Close the VBE and save the document AS A TEMPLATE ("Your File Name.dot).
  8. Close the file.
  9. Create a blank Word file and save it as a text (*.txt) file in the location designated in the code; close it.
 

Test the code:

  1. From Word's File menu select File > New and select your template.
  2. In the document that is created you should see the number in the place where you created the bookmark named oNum.
 

Sample File:

AutoNumber.zip 9.66KB 

Approved by mdmackillop


This entry has been viewed 169 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express