WD: Using WordBasic to Add Sequential Number to Form Document

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
  • 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 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:


    1. On the File menu, click New.


    2. Select Blank Document.


    3. Under "Create New," select Template.


    4. Click OK.


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


    1. Position the insertion point where you want to place the invoice number.


    2. On the Insert menu, click Form Field.


    3. For Type, click Text.


    4. Click Options.


    5. In Field Settings Bookmark: type Invoice_Number.


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


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


  10. 
         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 
  11. From the macro editor window, click Close on the File menu.


  12. 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. 100000
  5. Select the typed number only.


  6. On the Edit menu, click AutoText.


  7. In the "Make AutoText Available To" list, select "Documents Based on <template>."


  8. For Name, type INVOICE#.


  9. Click Add. (If Word prompts you with a question about Redefining the AutoText entry, click Yes.)


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

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



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 incremental automatically

Keywords : kbmacro kbmacroexample winword macword word6 word7 word95
Version : MACINTOSH:6.0,6.0.1; WINDOWS:6.0,6.0a,6.0c,7.0,7.0a
Platform : MACINTOSH WINDOWS
Issue type : kbhowto kbinfo


Last Reviewed: October 12, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.