Word Hacks [Electronic resources]

Andrew Savikas

نسخه متنی -صفحه : 162/ 110
نمايش فراداده

Hack 77 Number Documents Sequentially

Many businesses use numbers to track forms such as invoices and purchase orders. Those numbers usually must go in order and can't repeat. This hack shows you how to use Word to keep track of the numbering for you.

If you need to generate invoices for a business, you might save time using a premade invoice template, such as the ones available from the Microsoft web site. One such template is pictured in Figure 8-14.

Figure 8-14. You can download an assortment of premade templates from Microsoft.com

The template uses MACROBUTTON fields [Hack #70] to mark the items you replace when filling out the templatefor example, the invoice number field, which you replace with the correct number manually.

But how do you remember the last number you used? What if multiple users create invoices from the same template? A quick change to the invoice field and an AutoMacro [Hack #60] will give you a self-sequencing invoice template.

Create your own new template, either by downloading the Invoice template from Microsoft's web site (you can get there quickly in Word 2003 from the New Document Task Pane [Hack #9]) or by saving a new, blank document as a template.

Put your cursor at the spot in your template where you want the invoice number to appear. Select InsertField, choose DocVariable, and click the Field Codes button to display the dialog box shown in Figure 8-15. Enter InvoiceNumber in the "Field codes" box, as shown in Figure 8-15.

Figure 8-15. Inserting a DOCVARIABLE field

The next section describes the macro code used to increment the number.

8.9.1 The Code

Select ToolsMacroVisual Basic Editor and insert the following macro in your template:

Sub AutoNew( )
Dim sINIFile As String
Dim sCurrentNumber As String
sINIFile = "C:\InvoiceTemplate.ini"
sCurrentNumber = System.PrivateProfileString(sINIFile, _
"CurrentInvoice", "Number")
If Len(sCurrentNumber) = 0 Then
sCurrentNumber = CStr(1)
End If
ActiveDocument.Variables("InvoiceNumber") = sCurrentNumber
ActiveDocument.Fields.Update
sCurrentNumber = CStr(CInt(sCurrentNumber) + 1)
System.PrivateProfileString(sINIFile, "CurrentInvoice", _
"Number") = sCurrentNumber
End Sub

This macro uses a Config file [Hack #67] named C:\InvoiceTemplate.ini to track the invoice number. If the file doesn't exist yet, the macro creates one and starts the numbering at 1. The macro then puts the number from the file into a document variable named InvoiceNumber, which is referenced by the field you inserted in the template. Document variables are similar to document properties (which you view by choosing FileProperties), but document variables can be created, modified, and deleted only from a macro.

Save the changes and close your template. Now select FileNew to create a new document based on the template. Each new document created from this template will have an invoice number one higher than the previous document.

If you need to modify the numbering, open the InvoiceTemplate.ini file with any text editor, as shown in Figure 8-16.

Figure 8-16. Editing the invoice number

If you want your numbering to start at 100, create a new document from the template to run the AutoNew macro and generate the InvoiceTemplate.ini file. Next, open InvoiceTemplate.ini in a text editor and change it to read Number=100. The next invoice based on the template will be numbered 100.