We're developing an invoice template for Word, which contains an invoice number in a field at the top of the page.

We would like the invoice number to auto-increment (from the last invoice number), and we would like the filename to be based on the invoice number.

They'll all be saved in the same directory.

  • I do not understand why someone has down voted you; this question is no worse than many others. Your question implies you hope to do this all in Word. I would use Access (which has auto-increment) so I could pull in things like customer name and address from tables and keep a usable record of my invoices. Finally I would output to Word if that was how I wanted to print my invoices. – Tony Dallimore Jan 24 '12 at 11:51
  • @TonyDallimore Some people are judgemental nazis, myself probably included... :-P – Steve Jan 27 '12 at 2:35
  • @Tony/Steve: I think it may have been voted down as the tone is as though someone is simply asking a forum member to make it for them. I posted a question where someone pointed this out to me, and I get it now. Post a question but don't treat people on here like their your butler. No offence. – RocketGoal Jan 27 '12 at 16:57
  • @Mike. I see your point. I did not read the question that way although, on reflection, with the remarks about filenames and use of a single directory perhaps I should have. I read it as a question about auto-incrementing in Word. I can think of some very clumsy ways of doing so but as I said I would not even try. To me, this looks to be an easy Access macro. – Tony Dallimore Jan 27 '12 at 17:23
 Sub GoGenInv()
    Selection.GoTo What:=wdGoToBookmark, Name:="BkMrk_InvNum"
    Selection.MoveRight Unit:=wdCharacter, Count:=10, Extend:=wdExtend
    Dim nInvNum As Variant
    nInvNum = Val(Selection.Text)
    nInvNum = nInvNum + 1
    Selection.Text = nInvNum
    Selection.GoTo What:=wdGoToBookmark, Name:="BkMrk_InvNum"
End Sub
  • Code only answers are appreciated, but can be subject to deletion. Can you please explain how and why this is the answer to the question? – Stephen Rauch May 26 '17 at 1:55
  • Please explain how this works, and how a user would use it. Please do not respond in comments; edit your answer to make it clearer and more complete. P.S. You don’t need to delete and re-post a post to fix a problem; just edit it. – Scott May 26 '17 at 1:58

I found this which I think will solve the problem:

Use an Autonew macro to add a sequential number to a document and save it with that number.

In the template from which you create the document, insert a bookmark named Order in the location where you want the sequential number to appear and create an AutoNew macro in the template, as follows:

Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _
        "MacroSettings", "Order")

If Order = "" Then
    Order = 1
    Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
        "Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub

If you don't need to display the number in the document, but just want to save it with a sequential number, there is no need to create the bookmark in the template and you should then delete the second last line of the code.

  • I said I could think of clumsy ways of doing this so I am pleased your website didn't recommend a smooth way that I had overlooked. Good luck if you can make this work but I still think this is a really difficult way of achieving your objective. – Tony Dallimore Jan 30 '12 at 11:53
  • 1
    10 seconds to copy and paste the macro, and the only pitfall is a text file is required to store the incremental number. Not difficult at all. Not intellectually elegant, but it works fine. – Steve Jan 31 '12 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.