Microsoft KB Archive/119990

{|
 * width="100%"|

WD: Using WordBasic to Add Sequential Number to Form Document

 * }

Q119990

-

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
 * Microsoft 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 professionals 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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please see the following page on the World Wide Web:

"http://www.microsoft.com/partner/referral/" For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

"http://support.microsoft.com/directory/overview.asp" 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:

 On the File menu, click New. Select Blank Document. Under "Create New," select Template. Click OK. 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:

 Position the insertion point where you want to place the invoice number. On the Insert menu, click Form Field.</li> For Type, click Text.</li> Click Options.</li> In Field Settings Bookmark: type Invoice_Number.</li> Click OK twice to insert the form field into the template.</li></ol>

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:

<pre class="CODESAMP">    Sub Main

End Sub Enter the following code between the existing Sub Main and End Sub lines as follows.

<pre class="CODESAMP">    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

 * 1) Open your Template.
 * 2) Unprotect your document if necessary (click Unprotect document on the Tools menu).
 * 3) Place the insertion point on a blank line and type a starting number, for example:
 * 4) Select the typed number only.
 * 5) On the Edit menu, click AutoText.
 * 6) In the "Make AutoText Available To" list, select "Documents Based on ."
 * 7) For Name, type INVOICE#.
 * 8) Click Add. (If Word prompts you with a question about Redefining the AutoText entry, click Yes.)
 * 9) 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:


 * 1) On the Tools menu, click Protect Document.
 * 2) Select Forms and then click OK.

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.

<pre class="CODESAMP">  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).