This very simple macro adds a number to a document and saves with the file name in the format of "inv1.docx", creating a very simple numbering system for invoices.
Step 1: Create a folder on your hard drive. Create a text file named invoice-number.txt in the folder. To begin with 1, leave the file blank.
To start with a specific number, add this to the text file and save, using one less than the desired number.
[InvoiceNumber]
Invoice=140072
Step 2: Open Word then press Alt+F11 to open the VB Editor.
Step 3: Expand Microsoft Word Documents then double click on ThisDocument. Paste the code below into ThisDocument. Change the file paths as needed.
Sub CreateInvoiceNumber() Invoice = System.PrivateProfileString("C:\Users\user\Documents\a\" & _ "invoice-number.txt", "InvoiceNumber", "Invoice") If Invoice = "" Then Invoice = 1 Else Invoice = Invoice + 1 End If System.PrivateProfileString("C:\Users\user\Documents\a\" & _ "invoice-number.txt", "InvoiceNumber", "Invoice") = Invoice ' Insert the number in the document ActiveDocument.Range.InsertBefore Format(Invoice, "#") ActiveDocument.SaveAs2 FileName:= _ "C:\Users\user\Documents\a\inv" & Format(Invoice, "#") & ".docx" _ , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _ AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _ EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _ :=False, SaveAsAOCELetter:=False, CompatibilityMode:=14 End Sub
Step 4: Run the macro.
To place the number in a specific position (this macro adds at the very beginning of the page), create a bookmark in the template named Invoice then use this code to insert the number:
ActiveDocument.Bookmarks("Invoice").Range.InsertBefore Format(Invoice, "#")
More Information
Function to Create a serial number (from VBOffice.net)
Leave a Reply
9 Comments on "Add Sequential Numbers to a Word Document"
Make sure the double quotes are straight quotes, not the fancy slanted quotes.
This is awesome. Worked first go and is a treat. I also added to save as a PDF so my staff can just enter the Invoice Detail and the number and docs are saved for them. Cheers
try using a text file, not a doc.
"To place the number in a specific position (this macro adds at the very beginning of the page), create a bookmark in the template named Invoice then use this code to insert the number:
ActiveDocument.Bookmarks(“Invoice”).Range.InsertBefore Format(Invoice, “#”)"
The macro is numbering the doc just fine, at the begining of the page. How do I create this bookmark? I can create bookmarks, but I don't understand where to add the code.
The code goes in the VB Editor, replacing this line: ActiveDocument.Range.InsertBefore Format(Invoice, "#").
It can either be in the normal.dot template or in the template you're using for the invoice. (It's obviously better to use it in the invoice template so it works on any computer where the template is used.)
As long as the bookmark name is the same as the one used in the macro (Bookmarks(“Invoice”)), it'll work.
It means an object the code is looking for doesn't exist. I'm guessing it's failing on this :
ActiveDocument.Bookmarks(“Invoicenan”).Range.InsertBefore Format(Invoice, “”)
Does the bookmark exist? is it called invoicenan?