Microsoft KB Archive/85905

{|
 * width="100%"|

WD: WordBasic Macro to Number New Documents in Sequence

 * }

Q85905

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows 95, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, version 6.0.1

-

SUMMARY
This article describes how to use WordBasic to produce a series of documents from a template. The file name of each document includes a number that increments with each new document created.

NOTE: This article refers to glossaries. In Word 6.x or later versions, glossaries are called AutoText. In this article, if you are using Word version 6.x or later, glossaries means AutoText.

MORE INFORMATION
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

The following sample macro creates three new documents named Inv1.Doc, Inv2.doc, and Inv3.doc, using a template called Invoice.dot. The first document includes a glossary item named &quot;INVOICE#&quot; that is defined as the number 1. Each time you save a new document created with Invoice.dot, the number stored under the glossary name INVOICE# increments by one.

Use the appropriate method for your situation.

Word 6.x, 7.x
  Sub MAIN ' Get previous invoice number and increment by one. number = Val(GetAutoText$(&quot;INVOICE#&quot;, 1)) + 1 ' Make number a string value and remove preceding space. InvoiceNum$ = LTrim$(Str$(number)) ' Set updated invoice number back to AutoText storage. SetAutoText &quot;INVOICE#&quot;, InvoiceNum$, 1 ' Save the template to save the updated AutoText. SaveTemplate ' Save the document with the updated ' invoice number for its name. FileSaveAs .Name = &quot;INV&quot; + InvoiceNum$ End Sub

Word 2.x
  Sub MAIN ' Get previous invoice number and increment by one. number = Val(GetGlossary$(&quot;INVOICE#&quot;, 1)) + 1 ' Make number a string value and remove preceding space. InvoiceNum$ = Right$(Str$(number), Len(Str$(number)) - 1) ' Set new invoice number back to AutoText storage. SetGlossary &quot;INVOICE#&quot;, InvoiceNum$, 1 ' Save the template to save the updated invoice number. SaveTemplate ' Save the document with the updated ' invoice number for its name. FileSaveAs .Name = &quot;INV&quot; + InvoiceNum$ End Sub NOTE: The sample macro above can be named AutoNew in a template, so that the macro runs whenever a new document is created based on the template. You can also run the macro whenever you need to save a new file. The SetGlossary and GetGlossary (SetAutoText and GetAutoText in Word 6.0) commands in the above macro have the context set to template; therefore, you must run the above macro from a document based on a template other than Normal.dot. The FileSaveAs file name is a combination of &quot;INV&quot; and the contents of the INVOICE# glossary entry. The macro saves the series number for the document to a template glossary entry named &quot;INVOICE#.&quot;

The glossary entry INVOICE# in the above macro specifies the starting number for the sequence of file names. If the INVOICE# glossary entry does not exist, then the first time you run the macro, INVOICE# glossary returns the number 1. To set the starting value for the INVOICE# glossary, follow these steps prior to running the macro:


 * 1) In a new document based on your custom template, type the initial number for the first file name in the sequence, and select it.
 * 2) On the Edit menu, choose Glossary (AutoText in Word 6.0).
 * 3) Type INVOICE# as the glossary name.
 * 4) Click Define.

For additional information, please see the following article in the Microsoft Knowledge Base:

"Q119990 Using WordBasic to Add Sequential Number to New Form Document"