The information in this article applies to:
- 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
With Microsoft WordBasic, you can produce a series of document forms with
a number that is incremented with each new document created. This may be
helpful if you are designing an invoice template form and you want to
automatically increment the invoice number. The following macro example
increments the value stored in the INVOICE# AutoText entry each time the
macro is run and places the result in a text form field called
Invoice_Number.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft support engineers can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/supportnet/refguide/
To create the sample macro, follow these steps:
- If you have not created a custom template for your forms document,
create a new template that will serve as the form template. To do this,
follow these steps:
a. On the File menu, click New.
b. Select Blank Document.
c. Under "Create New," select Template.
d. Click OK.
e. Save the new template.
- Make sure your custom template is open and is the active window in
Word. Insert a text form field with the name Invoice_Number. To do
this, follow these steps:
a. Position the insertion point where you want to place the invoice
number.
b. On the Insert menu, click Form Field.
c. For Type, click Text.
d. Click Options.
e. In Field Settings Bookmark: type "Invoice_Number" (without the
quotation marks).
f. Click OK twice to insert the form field into the template.
- On the Tools Menu, click Macro.
- In the Macro Name text box, enter a name for the macro.
NOTE: Macro names cannot contain spaces or punctuation.
You can name this macro AutoNew and store it in a protected
form template (protect the template for Forms). If the macro is named
AutoNew, Word runs the macro whenever you create a new document
based on the template.
- From the "Macros in" box, select the custom template name.
NOTE: The SetAutoText and GetAutoText statements in this macro
have the context set to template; therefore, you must run this macro
from a document based on a template other than Normal.dot.
- Click Create.
- In the macro editor window, you will see:
Sub Main
End Sub
- Enter the following code between the existing Sub Main and End Sub
lines as follows.
Sub MAIN
' Unprotect form so an AutoText entry can be changed.
ToolsUnprotectDocument
' Retrieve the contents of the INVOICE# AutoText entry and
' add 1 to it.
number = Val(GetAutoText$("INVOICE#", 1)) + 1
' Convert the number to a string variable.
InvoiceNum$ = Str$(number)
' Remove the preceding space.
Temp$ = LTrim$(InvoiceNum$)
' SET the contents of the INVOICE# AutoText entry.
SetAutoText "INVOICE#", Temp$, 1
' Set the contents of the Invoice form field.
SetFormResult "Invoice_Number", Temp$
' Re-Protect the current document for forms.
ToolsProtectDocument .Type = 2, .NoReset = 1
' Save the template.
SaveTemplate
End Sub
- From the macro editor window, click Close on the File menu.
- Click Yes to save changes to the macro you just created.
You can define the AutoText item INVOICE# from this example to specify the
starting number for the sequence. If the INVOICE# AutoText entry does not
exist, the first time you run this macro, INVOICE# is set to the number 1.
To Change the Starting INVOICE# Number
- Open your Template.
- Unprotect your document if necessary (click Unprotect document on the
Tools menu).
- Place the insertion point on a blank line and type a starting number,
for example:
100000
- Select the typed number only.
- On the Edit menu, click AutoText.
- In the "Make AutoText Available To" list, select "Documents Based on
<template>."
- For Name, type "INVOICE#" (without the quotation marks).
- Click Add. (If Word prompts you with a question about Redefining the
AutoText entry, click Yes.)
- Delete the text that was typed in the document template.
The AutoText entry (INVOICE#) has been set.
The SetFormResult statement inserts the sequence number into a text form
field named "Text1." You may need to change "Text1" to refer to a form
field name that exists in your template.
The template you create should be protected for forms. To do this, follow
these steps:
- On the Tools menu, click Protect Document.
- Select Forms and then click OK.
MORE INFORMATION
If your form is a document rather than a template, the number can be saved
in a document variable using the SetDocumentVar statement. For example,
the following macro stores a number in the document variable named
"number." Each time the macro runs, it increments the number by one.
To create this macro, follow the steps for creating a macro listed earlier
in this article.
Sub MAIN
number = Val(GetDocumentVar$("number")) + 1
number$ = LTrim$(Str$(number))
SetDocumentVar "number", number$
SetFormResult "Text1", number$
End Sub
You can assign this macro to a form field as an on-exit or on-entry macro.
This way, the contents of the Text1 form field are automatically
incremented when the form field is entered or exited.
Other methods for preserving values between sessions of a macro are as
follows:
- Use SetPrivateProfileString to store values to a private settings file.
- Use SetProfileString to store values in the Win.ini file.
- Use the WordBasic File Input/Output statements to write to a text file
(for example, Write or Print statements).
For information about how to do this in Word 97, please see the following
articles in the Microsoft Knowledge Base:
ARTICLE-ID: Q161309
TITLE : Word 97: Macro Increments Invoice Number To New Form
Document
ARTICLE-ID: Q85905
TITLE : WordBasic Macro to Number New Documents in Sequence
REFERENCES
"Microsoft Word Developer's Kit," version 6.0, pages 183-198
|