Creating sequentially numbered documents (such as invoices)

Article contributed by Doug Robbins

Use an Autonew macro to add a sequential number to a document and save it with that number.

In the template from which you create the document, insert a bookmark named Order in the location where you want the sequential number to appear and create an AutoNew macro in the template, as follows:

Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _
        "MacroSettings", "Order")

If Order = "" Then
    Order = 1
Else
    Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
        "Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub

If you don't need to display the number in the document, but just want to save it with a sequential number, there is no need to create the bookmark in the template and you should then delete the second last line of the code.

Note: You may see postings elsewhere which recommend using the registry instead of a text file to store the number. If you use the Registry in this way, the user is tied to one machine. For instance, if more than one user needs access to the up-to-date number, a text file can be saved in a shared area on the network; a Registry setting can't. 

Also, if users need to be able to log on to more than one machine and the text file is in their user area on the network, they can have access to it from any machine.

Even if it's stored locally, if they upgrade to a new machine one day, the text file can just be copied to their new machine (much easier than copying Registry settings).

Another point worth considering: if there is any possibility that the user might one day need to amend the number manually for any reason, amending a text file is easy; letting a user loose on the Registry is scary!

See also Sequentially numbering multiple copies of single document using a macro and Sequentially numbering multiple copies of single document using a mailmerge.