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.
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, "#")
Function to Create a serial number (from VBOffice.net)