如何在每次打印文档时增加一个表单字段?
我知道有人有单页 MS Word 收据文档。 一个标题包括 RECEIPT #
。此时,他会查找最后打印的收据上的号码,并手动将收据号码调整为该号码加一,然后再打印一份文档副本。
我认为可以通过使用保存数字的表单字段来改进这一点,然后每次打印文档时该数字都会增加一。我没有发现 MS Word 开箱即用地支持任何内容,但我认为可以使用 VBA 来完成。几年前,我必须使用这种语言进行编程,但我从未对 Word 和打印事件中的表单字段执行过任何操作。
谁能用一些可以做到这一点的示例代码为我指明正确的方向?调整数量后自动保存文档也将受到欢迎。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Word 允许控制 DocumentBeforePrint 事件,我认为这会给您所需的结果。将文本表单字段添加到文档本身后,将此代码添加到 ThisDocument VBA 声明部分:
然后编辑 Document_Open() 子项以读取:
最后,使用以下代码创建 DocumentBeforePrint 子项:
此代码将启动 Print对话框,打印后增加计数器并保存文档。
Cancel = True
行取消标准的“打印”对话框,以便代码不会尝试两次打印文档。 (还可以通过删除Dialogs(wdDialogFilePrint).Show
和Cancel = True
来在打印后增加计数器。)我认为值得一提的是,一旦代码就位,通过双击文档的表单字段(将包含收据编号的字段)并选择“数字”作为字段类型,然后输入需要打印的第一个收据编号作为“默认编号”来设置此过程。
输入
表单字段的默认值后,手动保存文档,然后将其关闭。现在,每次打开该表单时,表单字段的内容都会被分配给该变量,并且 DocumentBeforePrint 事件会在每次打印文档时递增该字段。如果用户需要重置该字段(由于打印机卡纸或其他一些不可预见的事件),他应该双击该字段,更改默认值,单击对话框中的“确定”,保存文档并关闭它(以清除输出先前为计数器变量分配的值)。和以前一样,打开文档将使其准备好打印和增量。希望这有帮助。
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:
Then edit the Document_Open() sub to read:
Finally, create the DocumentBeforePrint sub with the following code:
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 removingDialogs(wdDialogFilePrint).Show
andCancel = 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.