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)


Brantley says
Great code! My Invoice variable in my text document is not updating. I do not see any code that saves the text file after updating the Invoice Variable. Do you have any suggestions? Thank you.
Gopal Arora says
where and how to use this code
ActiveDocument.Bookmarks("Invoice").Range.InsertBefore Format(Invoice, "#")
Diane Poremsky says
This goes into a Word document that has a bookmark named Invoice.
Angus says
Just testing this out, but I keep getting a 448 error when I try to run it. It says "Named argument not found". I'm using a Mac BTW.
I've only changed the filepath - my text file is contained in the folder "LOL". Not really sure why it isn't working.
Sub CreateInvoiceNumber()
Invoice = System.PrivateProfileString("C:/Users/angusho/Downloads/LOL" & _
"invoice-number.txt", "InvoiceNumber", "Invoice")
If Invoice = "" Then
Invoice = 1
Else
Invoice = Invoice + 1
End If
System.PrivateProfileString("C:/Users/angusho/Downloads/LOL" & _
"invoice-number.txt", "InvoiceNumber", "Invoice") = Invoice
' Insert the number in the document
ActiveDocument.Range.InsertBefore Format(Invoice, "#")
ActiveDocument.SaveAs2 FileName:= _
"C:/Users/angusho/Downloads/LOL" & Format(Invoice, "#") & ".docx" _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=14
Diane Poremsky says
Best guess: it's the system.privateprofilesting. That might be windows only. I'll have to take a look at making it work on mac.
Lance says
very nice code, it works flawlessly for me after fidgeting around with it.
only have 1 small problem.
what if I want to start an invoice number from 0000?
It does not let me do that, it will always start at 1 instead of adding the other 3 digits and starting from 0001
Diane Poremsky says
If you use
[InvoiceNumber]
Invoice=0001
and remove the if lines below - and just use Invoice = Invoice + 1 does it work?
If Invoice = "" Then
Invoice = 1
Else
Invoice = Invoice + 1
End If
Jimbob says
I've got this to work, sort of. The only problem is that it doesn't overwrite the previous number. So the first invoice has 1, then the next has 21, then the next has 321, then 4321, etc.
Diane Poremsky says
If there is an invoice number in the file, it won't replace it, not. But you shouldn't have an invoice # in the file before running the macro, except maybe during testing.
Jimbob says
I'm struggling with this. Do I paste the macro into the VBA editor for the invoice-number.txt file, or a new word document? If I try to paste it into the invoice number.txt file, it says it can't save it with a macro. If I try to run it in a new file, I get a runtime error - "Method 'PrivateProfileString' of object 'System' failed. 🙁
Diane Poremsky says
The macro code goes into the VBA Editor in Word. Are you using windows or mac?
This goes in a text file:
[InvoiceNumber]
Invoice=140072
Krystal Williams says
THIS IS NOT SIMPLE
Diane Poremsky says
Where are you running into problems? It's not hard but once you get it working it should always work. The problem is nothing is built into Word, so you need to use macros.
Sarah says
This works for me all up until I try and change the location of the macro. I'm putting it into a table - so I created a bookmark, and added the code where you specified in a comment to another user. and now I just get the error message command failed 🙁 what am I doing wrong.
Jerome says
It doesn't seem to auto advance for me. It just keeps using the same number without changing the number in the text file.
IanC says
Really great tutorial thank you so much. However, and I apologise if I am doing something silly, how do I get the macro to run automatically when someone using the document tries to save it?
Thank you so much
Diane Poremsky says
you'd need to use a before save macro. Something like the macro at the end - but it would run on every save, which may not be what you want. I use a macro that gets the invoice # and saves (it does several things). I added a button to the ribbon that calls a macro that runs several macros:
Sub FinalizeInvoice()
CreateInvoiceNumber
CopyToExcel
FinalCleanup ' this does the save
End Sub
This is an automated macro - but it will run every time the file is saved.
saeed says
dear
I make a invoice when i open the invoice then number can not changed automatically. I want to change invoice number when i open my invoice document.
Diane Poremsky says
Are you using a template? If yes, it should use the next number. If not, you could use an auto start menu to update the number.
JKN says
I am trying to create a macro for outlook 2016 appointments that would add a number sequentially each time the appointment template was opened. I need to run this under public user so as others open it will give next number. Thank you in adance
here is form
Sequential Number Here
Outside
Assignment #: Click here to enter text.
Court Reporter Needed: Click here to enter text.
Job Type: Click here to enter text.
Referral: Click here to enter text.
Assignment Date: Click here to enter a date.
Contact: Click here to enter text.
Time: Click here to enter text.
Phone: Click here to enter text.
Atty: Click here to enter text.
Email: Click here to enter text.
Firm: Click here to enter text.
Attorney Address: Click here to enter text.
Who: Click here to enter text.
Where: Click here to enter text.
Case Name: Click here to enter text. vs. Click here to enter text.
Confirmed by:
Download
Files Watched
Encoded: Local S3
Encoding Downloaded
DVD Burned
Uploaded to Client
Watch DVD
Shipped
Soura_C says
Hi,
Is there a way to create 100 docx file without running the macro again and again and naming them in sequencing order.
Example: docx_1 having 1 as number
docx_2 having 2
docx_3 having 3
All of that without opening and running macro again and again
Diane Poremsky says
Yes, you can do things like that - if the docs exist in the folder, you'd change the macro to rename the files in the folder.
Tom says
First of all let me note that I'm a novice at this, never used the VB editor before. I'm making an invoice template for work, but I keep getting this error
"Compile error:
Expected line number or label or statement or end of statement"
I went over the code a hundred times, but I can't seem to figure out how to get it right, I hope you can help me. Here's the code I used:
Sub CreateInvoiceNumber()
Invoice = System.PrivateProfileString("C:UserstomheffelsDocumentsInvoice AGinvoice-number.rtf", "InvoiceNumber", "Invoice")
If Invoice = "" Then
Invoice = 1
Else
Invoice = Invoice + 1
End If
System.PrivateProfileString("C:UserstomheffelsDocumentsInvoice AGinvoice-number.rtf", "InvoiceNumber", "Invoice") = Invoice
ActiveDocument.Range.InsertBefore Format(Invoice, "#")
ActiveDocument.SaveAs2 fileName:= _
“C:UserstomheffelsDocumentsInvoiceAGinv" & Format(Invoice, “#”) & “.pdf” _
, FileFormat:=wdFormatPDF, LockComments:=False, Password:=““, _
AddToRecentFiles:=True, WritePassword:=““, ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=14 _
End Sub
Diane Poremsky says
Make sure the double quotes are straight quotes, not the fancy slanted quotes.
Rycker says
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
Jennifer says
I'm getting the following Runtime error msg: Method "PrivateProfileString" of object "System" failed. Can you tell me what I did wrong? Here's the code I inserted in macro below (its for a House Roster, not an invoice,so I changed text accordingly):
Sub CreateHouseRosterNumber()
HouseRoster = System.PrivateProfileString("C:UsersuserDocumentsa" & _
"HouseRoster-number.docx", "HouseRoster", "Roster")
If HouseRoster = "" Then
HouseRoster = 1
Else
HouseRoster = HouseRoster + 1
End If
System.PrivateProfileString("C:UsersJennifer BaumannDocumentsa" & _
"HouseRoster-number.docx", "HouseRoster", "Roster") = HouseRoster
' Insert the number in the document
ActiveDocument.Range.InsertBefore Format(HouseRoster, "#")
ActiveDocument.SaveAs2 FileName:= _
"C:UsersJennifer BaumannDocumentsaHouseRoster" & Format(HouseRoster, "#") & ".docx" _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=14
End Sub
Diane Poremsky says
try using a text file, not a doc.
Maria says
"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.
Diane Poremsky says
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.
Ellen says
Using the code below, I keep getting an error that says "The requested member of the collection does not exist." Do you know what I am doing wrong?
Sub CreateInvoiceNumber()
Invoice = System.PrivateProfileString("C:\Users\Chad\Documents\BOLTemplate\" & _
"invoice-number.txt", "InvoiceNumber", "Invoice")
If Invoice = "" Then
Invoice = 1
Else
Invoice = Invoice + 1
End If
System.PrivateProfileString("C:\Users\Chad\Documents\BOLTemplate\" & _
"invoice-number.txt", "InvoiceNumber", "Invoice") = Invoice
' Insert the number in the document
ActiveDocument.Bookmarks(“Invoicenan”).Range.InsertBefore Format(Invoice, “”)
ActiveDocument.SaveAs2 FileName:= _
"C:\Users\Chad\Documents\BOLTemplate\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
Diane Poremsky says
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?