WD: WordBasic Macro to Number New Documents in Sequence

Last reviewed: August 27, 1997
Article ID: Q85905
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a
  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Word for the Macintosh, versions 6.0, 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 "as is" 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 "INVOICE#" 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$("INVOICE#", 1)) + 1
      ' Make number a string value and remove preceding space.
      InvoiceNum$ = LTrim$(Str$(number))
      ' Set updated invoice number back to AutoText storage.
      SetAutoText "INVOICE#", InvoiceNum$, 1
      ' Save the template to save the updated AutoText.
      SaveTemplate
      ' Save the document with the updated
      ' invoice number for its name.
      FileSaveAs .Name = "INV" + InvoiceNum$
   End Sub

Word 2.x

   Sub MAIN
      ' Get previous invoice number and increment by one.
      number = Val(GetGlossary$("INVOICE#", 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 "INVOICE#", 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 = "INV" + 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 "INV" and the contents of the INVOICE# glossary entry. The macro saves the series number for the document to a template glossary entry named "INVOICE#."

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#" (without the quotation marks) as the glossary name.

  4. Click Define.

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

   ARTICLE-ID: Q119990
   TITLE     : Using WordBasic to Add Sequential Number to New Form
               Document

REFERENCES

"Microsoft Word Developer's Kit," version 6.0, page 185ff

"Using WordBasic," by WexTech Systems and Microsoft, pages 84-87, 283, 228


Additional query words: automatic automatically 6.0.1 7.0 auto serial 1.0
1.1 1.1a 2.0 2.0a 1.0 1.10 1.0a 2.0 2.0a
Keywords : kbcode kbmacro kbprg kbmacroexample winword macword word6 word7 word95 winword2 kbmacro kbusage
Version : WINDOWS:1.x, 2.x, 6.x, 7.0, 7.0a; MACINTOSH: 6.0, 6.0.1
Platform : MACINTOSH WINDOWS
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 27, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.