WD: WordBasic Macro to Number New Documents in Sequence
ID: 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 "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:
- In a new document based on your custom template, type the initial number
for the first file name in the sequence, and select it.
- On the Edit menu, choose Glossary (AutoText in Word 6.0).
- Type INVOICE# as the glossary name.
- 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
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 2.0 1.0a
Keywords : kbmacroexample
Version : MACINTOSH:6.0.1; WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a
Platform : MACINTOSH WINDOWS
Issue type : kbinfo