How can I increase a form field every time the document is printed?
I know someone which has a single-page MS Word document for receipts.
One headline includes RECEIPT #<number>
. At the moment he looks up which number was on the last printed receipt and adjust the receipt number manually to be that number plus one before he prints out a single copy of the document.
I thought this could be improved by using a form field holding the number which is then increased by one every time the document is printed. I didn't found anything supported by MS Word out-of-the-box, but I think that it can be done using VBA. It's years ago that I开发者_如何学C had to program in this language and I never did anything with form fields in Word and print events.
Can anyone point me in the right direction with some sample code which can do this? Automatic saving of the document after adjusting the number would be welcome, too.
Word lets one control the DocumentBeforePrint event, which I think would give you the result you need. After a Text Form Field has been added to the document itself, add this code to the ThisDocument VBA Declarations section:
Option Explicit
Private WithEvents app As Application
Then edit the Document_Open() sub to read:
Private Sub Document_Open()
Set app = Application
ActiveDocument.Variables("ReceiptNumber").Value = ActiveDocument.FormFields(1).Result
End Sub
Finally, create the DocumentBeforePrint sub with the following code:
Private Sub app_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dialogs(wdDialogFilePrint).Show
With ActiveDocument
.Variables("ReceiptNumber").Value = .Variables("ReceiptNumber").Value + 1
.FormFields(1).Result = .Variables("ReceiptNumber").Value
.Save
End With
Cancel = True
End Sub
This code will launch the Print dialog box and, after printing, increment the counter and Save the document. The Cancel = True
line cancels the standard Print dialog box, so that the code does not try to print the document twice. (One can also increment the counter after printing by removing Dialogs(wdDialogFilePrint).Show
and Cancel = True
.)
I think it's worth mentioning that once the code is in place, set this process up by double-clicking in the document's form field (the one that will contain the receipt number) and select "Number" for the field type and enter the first receipt number that needs to be printed as the "Default number."
Once the form field's defaults are entered, save the document manually and then close it. Now each time it is opened, the contents of the form field will be assigned to the variable and the DocumentBeforePrint event will increment the field upon each printing of the document. Should the user need to reset the field (due to printer jam or some other unforeseen event), he should double-click the field, change the default value, click OK in the dialog box, save the document, and close it (to clear out the previously assigned value for the counter's variable). As before, opening the document will make it ready for printing and incrementing. Hope this helps.
精彩评论