WD: Using WordBasic to Add Sequential Number to Form Document

Last reviewed: February 3, 1998
Article ID: 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
  • 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:

  1. 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.

  2. 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.
    
    

  3. On the Tools Menu, click Macro.

  4. 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.

  5. 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.

  6. Click Create.

  7. In the macro editor window, you will see:

    Sub Main

    End Sub

  8. 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
    
    

  9. From the macro editor window, click Close on the File menu.

  10. 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:

          100000
    

  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 <template>."

  7. For Name, type "INVOICE#" (without the quotation marks).

  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.

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


Additional query words: form text field macro SetFormResult auto serial
increment sequential seq sequence increment incremental automatically
Keywords : kbmacroexample macword winword word6 word7 word95 kbmacro
Version : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a; MACINTOSH:6.0,6.0.1
Platform : MACINTOSH WINDOWS
Issue type : kbhowto 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: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.