WD: WordBasic Macro to Number New Documents in SequenceLast reviewed: August 27, 1997Article ID: Q85905 |
The information in this article applies to:
SUMMARYThis 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 INFORMATIONWARNING: 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 SubNOTE: 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:
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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |