# [SOLVED] Creating Sequentially numbered documents in Word 2007



## alantz (Apr 6, 2009)

Currently we have rental forms that get filled out everytime one of our rental vehicles gets rented. In the past these forms have come pre-numbered from a local printing company. To save the company money, I was going to re-create the form in Word so that we didn't have to order them. Also, this way the form will look much tidier as the information will not have to be handwritten in. The only problem is, how do I add a sequential number to the top of the form. I need the first form to be say 03051, and then everytime the form is opened and saved thereafter, I need it to sequentially go up in numbers. 
If this is even possible, I would only like it to give it the next number if the form is actually saved, not just opened, as sometimes the form will be opened, but not saved, and in that case I don't want it to get the next number.

Any help would be much appreciated 

Alanda


----------



## macropod (Apr 11, 2008)

*Re: Creating Sequentially numbered documents in Word 2007*

Hi Alanda,

To do this, should use a document template with some vba code to:
. run upon creation of a new document
. open a nominated text file containing the document number,
. insert the number into the new document (eg insert the number into a Custom Document Property that can then be referenced via a DOCPROPERTY field in the document)
. increment the number in the text file, then close it.

Although you could simply increment a number in an existing document, you really should use a template (ie .dot file) as the base from which your agreements are made.


----------



## alantz (Apr 6, 2009)

*Re: Creating Sequentially numbered documents in Word 2007*

That all sounds like a great idea, but unfortunately I am an amateur when it comes to VBA code. Could you show me how to write the code, and how to put it into the document.?


----------



## macropod (Apr 11, 2008)

*Re: Creating Sequentially numbered documents in Word 2007*

Hi Alanda,

Sorry about the delay in replying.

Here's some code for inserting and incrementing a number from a text (ini) file:

```
Sub AutoNew()
Dim InvoiceFile As String
Dim InvNum As String
'Save ini file in the Word startup folder.
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, by using the following line, the Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
'If there is no InvoiceNumber reference in the ini file
'Create one and set the number to 1, otherwise increment the number
If InvNum = "" Then
    InvNum = 1
Else
    InvNum = InvNum + 1
End If
System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = InvNum
With ActiveDocument
  .CustomDocumentProperties("InvNum") = InvNum
  .Fields.Update
End With
End Sub
```
To use the code, you need to open your invoice template and add the macro to it. For instructions on how to do this, see: Installing Macros.

Then, go to Office Button|Info|Properties|Custom and add a document property named 'InvNum'. Next, in the body of the invoice template, position the insertion point where you want the invoice number to appear and press Ctrl-F9 to create a pair of field braces (ie '** }') and type 'DOCPROPERTY InvNum' between the field braces, thus '{DOCPROPERTY InvNum}'. If you want to force the Invoice # to display leading 0s (eg 01234), add '\# 0000' to the field code, thus: '{DOCPROPERTY InvNum \# 0000}'.

Finally, save the template as a Word document Template (ie with a '.dot' extension, via File|Save As), named 'Invoice' (or another suitable name).

Now, whenever you want to create a new Invoice, simply go to File|New and select 'Invoice' (or your preferred name). You'll get an invoice with the next available number. If you need to set the initial invoice #, or reset it, you'll find a file named 'Invoice.ini' in your Word Startup folder (to find where this is stored, look under Tools|Options|File Locations). In case you need it, there's a commented-out line to store the 'Invoice.ini' in your Word Workgroup folder instead, so that others in your workgroup can access it also.


----------



## alantz (Apr 6, 2009)

*Re: Creating Sequentially numbered documents in Word 2007*

I really appreciate the help you have given me on this issue. I think I have almost got this invoice numbering thing working. Right now, everytime I open the file, it gives it the next number, even if I say 'NO' to saving it. Is there any way that if a person answers 'NO' to the prompt when it asks you if you want to save the document, then the next time you open the document, it will give it that same number again, rather than jumping to the next number?
Also how do I make the document numbering start at 03101?


----------



## macropod (Apr 11, 2008)

*Re: Creating Sequentially numbered documents in Word 2007*

Hi Alanda,

Unless you want a prompt every time an invoice document is created, there's really no practical way to prevent the number from incrementing - it's done when the document is created.

However, as indicated in my previous post, if you need to set the initial invoice #, or reset it, you'll find a file named 'Invoice.ini' in your Word Startup folder (to find where this is stored, look under Tools|Options|File Locations). Simply open that file with notepad (eg by double-clicking on it) and make the invoice # whatever you want it to be.

If you want to do some experimentation with your Invoce document (eg to change the layout), you could simply rename 'Invoice.ini' temporarily (eg 'xInvoice.ini'). Word will create a new one, starting at # 1, but you can delete this when you're finished and change the 'xInvoice.ini' file's name back to 'Invoice.ini' again, so that the invoice numbering picks up from where it left off prior to the experimentation.


----------



## alantz (Apr 6, 2009)

*Re: Creating Sequentially numbered documents in Word 2007*

This is going to sound like a really silly question, but where is the Word Startup folder? I am having a heck of a time trying to figure this out. When you say to look under Tools/Options/File Locations, I don't know where you mean. Which tools are you refering to?
Also a prompt wouldn't be such a bad thing in this case. When the user goes to save the form, would it be possible then to put the prompt asking the user if they want to assign this form the next number? If they say no, then it doesn't assign the next number? If this is possible, how would you do it?
Thanks again for your time.


----------



## macropod (Apr 11, 2008)

*Re: Creating Sequentially numbered documents in Word 2007*

Hi Alanda,

Look in Word's menu, under Tools|Options|File Locations!

As forthe prompt, I think you're missing the point that the 'Invoice.ini' file gets updated with the next number immediately the document is created. Thus, it's not a case of being able to prompt whether to increment the number at the time the file gets saved.

What you could do instead is add the following macro to your Normal.dot template:

```
Sub RestInvoiceNumber()
Dim InvoiceFile As String
Dim InvNum As String
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, if using a Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
InvNum = Trim(InputBox("What is the last valid Invoice Number?" & vbCrLf & _
  "The current number is: " & InvNum))
If IsNumeric(InvNum) Then
  System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = CInt(InvNum)
  End
Else
  If InvNum = "" Then End
End If
MsgBox "Renumbering error, please try again."
End Sub
```
This enables you to re-set the Invoice Number if you need to, without manually finding & editing the 'Invoice.ini' file.


----------

