Articles | Photoshop blog | Photography blog | about me | e-mail

Helen Bradley - MS Office Tips, Tricks & Tutorials

Sunday, February 18, 2007

Autonumber a series of Word documents

Word does not contain any option for automatically numbering a series of documents with a consecutive number - the type of thing you might want to do if you are using Word to create invoices or numbered purchase orders. The solution is to create a macro to do the work for you.

Start with a template that has a macro that runs when ever the template is used for a new document. The macro should read a number stored in a file on your drive, add it to your document and then, to prepare the number for the next time it's required, the number should be incremented by one and be written back into the file.

To create the solution, create a new document (or open an existing one to use as a template) and click where you want the sequential number to appear, and choose Insert, Bookmark, type docNum in the Bookmark name area and click Add.

Save this file as a template by choosing File, Save As, from the Files as type list choose Document Template (*.dot), give the file a name and click Save. With the file still open, choose Tools, Macro, Macros and type the name of the macro docNum and, from the Macros In list choose the template file name for the file you just saved and choose Create.

Type this macro as shown, the sub and end sub lines should be there already:

Sub docNum()
Dim MyString, docNumber
FileToOpen = "c:\windows\docNumfile.txt"
Open FileToOpen For Input As #1
Input #1, docNumber
Close #1 ' Close file
ActiveDocument.Bookmarks("docNum").Select
Selection.InsertAfter Text:=docNumber
docNumber = docNumber + 1
Open FileToOpen For Output As #1
Write #1, docNumber
Close #1 ' Close file.
End Sub

Now choose File, Close and Return to Microsoft Word. With the template on the screen, choose File, Close and answer Yes when prompted to save your changes.

Now open Notepad and type a number 4 or 5 numbers less than the number of the first quote you want to use. So, if you want to start numbering at 200, type 195 so you have a few numbers to use to test the process. Choose File, Save As and save the file as a text file, calling it docNumfile.txt and save it to this folder: C:\windows. Close Notepad

To test the process, choose File, New, choose the template file and click OK. Now run the macro by choosing Tools, Macro, Macros, docnumb, Run. If you have everything right the document number will be inserted in the document.

When this is working fine, alter the macro so this process of inserting the document number happens automatically whenever you create a new document based on this template. To do this, choose Tools, Macro, Macros, click on docnumb and click Edit. Change this macro's procedure name by altering this line of code:

Sub docnumb()

to read

Sub AutoNew()

Choose File, Close and Return to Microsoft Word. With the template on the screen, choose File, Close. Say No to saving your changes to this file but answer Yes to save the changes to your template file.

Now test again by creating a new file using File, New, choose your template and click OK. The document number should be added automatically to the new document.

If, in the process of testing you find you go past your starting document number, open Notepad and open the file docnumfile.txt, type a new starting number and save it again.

Labels: , , , , ,

Add to Technorati Favorites

10 Comments:

At April 11, 2007 at 10:36 AM , Blogger Eagleville said...

I would like to create a series of file labels with consecutive numbers as in "Label 1", "Label 2" etc. How can this be done in Word or Excel?
Ken

 
At April 16, 2007 at 8:36 PM , Blogger Jane said...

In this case Microsoft Word is probably your best option as it has a built in tool for creating labels. To do this, launch Word and choose Tools, Letters and Mailings, Envelopes and Labels and click the Labels tab. Click Options to open the Label Options dialog and select the label type to use. Click Ok. Enable the full page of the same label option and click New Document to create a set of labels in a brand new document.

To add your numbers type the word Label and space, and then choose Insert, Field, from the Categories list choose Numbering and from the Field Name list choose SEQ. After the field code SEQ type mynum (or a single word identifier for your sequence). Press the spacebar once, click Options, click the Field Specific Switches tab, select the \r switch and click Add to Field. After the \r entry in the Field Codes area type the number 1 as the starting number for your sequence and click Ok and Ok again.

Back in the document select the next label and click where the label number should appear. Type the word Label and press space. This time choose Insert, Field and choose the Numbering option from the Category list and the SEQ option from the Field Names list. After the word SEQ type the identifier mynum (or the identifier that you used previously). Tap the spacebar, click Options, click the Field Specific Switches tab, click the \n option and click Add to field and then click Ok twice.

You can now copy and paste this second field code into each of the labels on the page. To update the label numbering, choose Edit, Select All and press the F9 key. You’ll now have a series of sequentially numbered labels. At any time you can alter the first field code by changing the number after the \r option to start at a new number thus giving you a sequential series of labels from a single saved document. To change the field code, click on it, press Shift + F9, change the value, press Shift + F9 again then select the entire document and press F9 to update it.

 
At November 29, 2007 at 10:56 AM , Anonymous Anonymous said...

Thanks for the autonumber, but have one request. How can I add to the macro a style change (the numbers need to be style as page number)

thanks

 
At January 24, 2008 at 6:57 AM , Anonymous Anonymous said...

I would like to make a Purchase Order form with two copies per page that both have different numbers. How can I make two different macros that run similar functions on the page?

 
At April 17, 2008 at 7:35 AM , Anonymous Anonymous said...

Thanks for the great tip! I was able to create my autonumbering document with your directions. But, you have typed "docnumb", which should be "docNum" correct?. Also, for those not familiar with visual basic, it would be helpful to show the entire final programming script, as I had difficulty figuring out how that last bit of programing fit into the final script. It was hit and miss for a bit. Thanks again!

 
At September 18, 2008 at 3:17 PM , Anonymous Anonymous said...

Excellent documentation!!!
Keep on going :)

 
At October 13, 2008 at 2:51 PM , Anonymous Anonymous said...

I am trying to create a sequential numbering macro for a work order form. I want it to display the document number but I don't want anyone to be able to edit the number itself. If I try and protect the document every vb code I have used will not run the macro. It tells me that it can't run in a protected area of the document. Any help on this would be greatly appreciated.

 
At January 24, 2009 at 5:42 PM , Anonymous Anonymous said...

I tried your macro for automatically numbering a document. However, each time I try to use it, I get an error message and the macro comes up to debug. What am I doing wrong?

Jerry

 
At July 12, 2009 at 3:23 PM , Anonymous Anonymous said...

thank you

Carlos Martinez
Colombia

 
At April 19, 2010 at 8:29 AM , Anonymous Anonymous said...

Thanks for this but I would like start numbering from 00001 upwards. I have tried this code but just get it to go from 1 upwards. How can I do this please?

 

Post a Comment

Please feel free to add your comment here. Thank you!

Subscribe to Post Comments [Atom]

<< Home