@Poremsky.com

Tips & Tricks for Windows, Office, and Whatever

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

Add Sequential Numbers to a Word Document

Published on April 16, 2014 Last reviewed on February 20, 2020

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:

  • Share on Facebook (Opens in new window) Facebook
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on X (Opens in new window) X
  • Print (Opens in new window) Print

Comments

  1. Brantley says

    August 6, 2019 at 6:39 pm

    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.

    Loading...
  2. Gopal Arora says

    July 31, 2019 at 2:32 am

    where and how to use this code
    ActiveDocument.Bookmarks("Invoice").Range.InsertBefore Format(Invoice, "#")

    Loading...
    • Diane Poremsky says

      July 31, 2019 at 7:05 am

      This goes into a Word document that has a bookmark named Invoice.

      Loading...
  3. Angus says

    July 8, 2019 at 5:08 pm

    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

    Loading...
    • Diane Poremsky says

      July 11, 2019 at 8:46 pm

      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.

      Loading...
  4. Lance says

    July 5, 2019 at 4:56 am

    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

    Loading...
    • Diane Poremsky says

      July 5, 2019 at 12:35 pm

      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

      Loading...
  5. Jimbob says

    June 11, 2019 at 3:23 am

    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.

    Loading...
    • Diane Poremsky says

      June 11, 2019 at 7:31 pm

      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.

      Loading...
  6. Jimbob says

    June 10, 2019 at 10:36 am

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

    Loading...
    • Diane Poremsky says

      June 11, 2019 at 7:28 pm

      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

      Loading...
  7. Krystal Williams says

    May 29, 2019 at 2:22 am

    THIS IS NOT SIMPLE

    Loading...
    • Diane Poremsky says

      May 29, 2019 at 11:20 am

      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.

      Loading...
  8. Sarah says

    August 3, 2018 at 6:51 am

    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.

    Loading...
  9. Jerome says

    July 27, 2018 at 8:43 pm

    It doesn't seem to auto advance for me. It just keeps using the same number without changing the number in the text file.

    Loading...
  10. IanC says

    June 20, 2018 at 11:06 am

    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

    Loading...
    • Diane Poremsky says

      August 2, 2018 at 4:39 pm

      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.

      Private WithEvents App As Word.Application
      
      Private Sub Document_Open()
      Set App = Word.Application
      End Sub
      
      Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
      CreateInvoiceNumber
      End Sub

      Loading...
      • saeed says

        October 11, 2018 at 6:34 am

        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.

        Loading...
        • Diane Poremsky says

          November 1, 2018 at 4:39 pm

          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.

          Loading...
  11. JKN says

    February 16, 2018 at 4:28 pm

    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

    Loading...
  12. Soura_C says

    August 25, 2017 at 3:18 am

    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

    Loading...
    • Diane Poremsky says

      November 1, 2018 at 4:37 pm

      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.

      Loading...
  13. Tom says

    July 23, 2017 at 9:48 am

    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

    Loading...
    • Diane Poremsky says

      August 9, 2017 at 10:29 pm

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

      Loading...
  14. Rycker says

    April 18, 2017 at 6:21 am

    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

    Loading...
  15. Jennifer says

    February 24, 2017 at 9:25 am

    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

    Loading...
    • Diane Poremsky says

      February 26, 2017 at 4:30 pm

      try using a text file, not a doc.

      Loading...
  16. Maria says

    May 4, 2016 at 3:00 am

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

    Loading...
    • Diane Poremsky says

      May 8, 2016 at 6:11 am

      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.

      Loading...
  17. Ellen says

    October 22, 2015 at 3:52 pm

    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

    Loading...
    • Diane Poremsky says

      October 22, 2015 at 5:16 pm

      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?

      Loading...

Recent Posts

  • How to Remove Office Licenses from Windows
  • Use a JavaScript bookmarklet to open an archive for a webpage
  • How to fix records that are all in one column
  • Change Password or Pin Complexity after removing from Intune
  • Server drafts location is not valid error message

Recent Comments

  • Hornblower409 on How to Remove Office Licenses from Windows
  • FUAD on Remove Office Licenses from Windows
  • Brian DuBridge on Add the Recent Items Folder to Windows 10 Quick Access
  • Renee Moodie on Remove Office Licenses from Windows
  • John P on Remove Office Licenses from Windows
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.

To find out more, including how to control cookies, see here: Cookie Policy

© 2026 · @Poremsky.com

Go to mobile version
%d