@Poremsky.com

Tips & Tricks for Windows, Office, and Whatever

  • Home
  • Windows
  • Office
  • BlackBerry
  • Web Design
  • WordPress
  • Internet Explorer

Add Sequential Numbers to a Word Document

Published on April 16, 2014 Last reviewed on August 9, 2017

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.

Invoice Number example

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

VBA Editor

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)

Share this:

  • Facebook
  • LinkedIn
  • Twitter
  • Google
  • Print

Related posts:

Create buttons for Word's Building Blocks Create sequential numbers or random character keywords Fewer steps to open or save files in Office 2013 Can't Preview Word 2010 Docs in Windows 7

Leave a Reply

9 Comments on "Add Sequential Numbers to a Word Document"

Notify of
2500

2500

Sort by:   newest | oldest | most voted
Tom
Share On TwitterShare On Google
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… Read more »
Vote Up0Vote Down  Reply
July 23, 2017 9:48 am
Diane Poremsky
Share On TwitterShare On Google

Make sure the double quotes are straight quotes, not the fancy slanted quotes.

Vote Up0Vote Down  Reply
August 9, 2017 10:29 pm
Rycker
Share On TwitterShare On Google

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

Vote Up0Vote Down  Reply
April 18, 2017 6:21 am
Jennifer
Share On TwitterShare On Google
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,… Read more »
Vote Up1Vote Down  Reply
February 24, 2017 9:25 am
Diane Poremsky
Share On TwitterShare On Google

try using a text file, not a doc.

Vote Up0Vote Down  Reply
February 26, 2017 4:30 pm
Maria
Share On TwitterShare On Google

"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.

Vote Up0Vote Down  Reply
May 4, 2016 3:00 am
Diane Poremsky
Share On TwitterShare On Google

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.

Vote Up0Vote Down  Reply
May 8, 2016 6:11 am
Ellen
Share On TwitterShare On Google
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
Vote Up0Vote Down  Reply
October 22, 2015 3:52 pm
Diane Poremsky
Share On TwitterShare On Google

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?

Vote Up0Vote Down  Reply
October 22, 2015 5:16 pm

Recent Posts

  • Add a Confirm Unsubscribe Link in Sendy
  • Add the Recent Items Folder to Windows 10 Quick Access
  • A New Scam? Cashing in on Kohl's Cash
  • Windows 10: Pin Option Not Available at Logon
  • Edit Windows Mail Custom Dictionary

Recent Comments

  • Diane Poremsky on Use local templates with Office 2013
  • Darren O\'Leary on Use local templates with Office 2013
  • billiejo r lear on A New Scam? Cashing in on Kohl's Cash
  • Diane Poremsky on Create vCards from a CSV file
  • Rajesh Jaddu on Create vCards from a CSV file

© 2018 · @Poremsky.com