如何让WORD在打印时自动加上打印份数编号 下载本文

如何让WORD在打印时自动加上打印份数编号

by nosper on 一月 5th, 2011

问题的提出: 老婆所在的公司需要做2011年整年的文档和表格,里面的编号随着打印份数自动更新:比如需要打印100份,每份编号则按顺序从 0001排到 0100。

在网上google 了一下,也有不少网友提出了类似的问题:

“公司有一份调查表,需要打印100份,每份都要有一个编号,从000001到000100。如何让WORD在打印时自动加上打印份数编号?”

这个需要用到word 的宏操作,感觉它和ps里面的action 一样,就是可以让用户自定义一些操作,让宏来重复执行。word2007 有宏录制功能(在view视窗栏里面)。

方法一:宏循环嵌套

先手动几次:改编号——打印——改下一个编号——再打印, 让宏来记录这些动作。然后查看这些基本动作的宏代码,在里面加入循环和嵌套。 经过自己几次尝试和修改,得到如下宏代码:

Sub PrintCopies() '

' Macro1 Macro ' '

Dim i As Long Dim lngStart Dim lngCount lngCount = InputBox(\enter the number of copies you want to print\to print\

If lngCount = \ Exit Sub End If

lngStart = InputBox(\to print\\the starting number you want to print\1)

If lngStart = \ Exit Sub End If

For i = lngStart To lngCount If i < 10 Then

Selection.TypeText Text:=\ Application.PrintOut FileName:=\Range:=wdPrintAllDocument, Item:= _

wdPrintDocumentContent, Copies:=1, Pages:=\PageType:=wdPrintAllPages, _

ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _

False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _

PrintZoomPaperHeight:=0 End If

If (i >= 10) And (i < 100) Then Selection.TypeText Text:=\ Application.PrintOut FileName:=\Range:=wdPrintAllDocument, Item:= _

wdPrintDocumentContent, Copies:=1, Pages:=\PageType:=wdPrintAllPages, _

ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _

False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _

PrintZoomPaperHeight:=0 End If

If (i >= 100) And (i < 1000) Then Selection.TypeText Text:=\ Application.PrintOut FileName:=\Range:=wdPrintAllDocument, Item:= _

wdPrintDocumentContent, Copies:=1, Pages:=\PageType:=wdPrintAllPages, _

ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _

False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _

PrintZoomPaperHeight:=0 End If

If (i >= 1000) And (i < 10000) Then Selection.TypeText Text:=i

Application.PrintOut FileName:=\Range:=wdPrintAllDocument, Item:= _

wdPrintDocumentContent, Copies:=1, Pages:=\PageType:=wdPrintAllPages, _

ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _

False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _

PrintZoomPaperHeight:=0 End If

Selection.TypeBackspace Selection.TypeBackspace Selection.TypeBackspace Selection.TypeBackspace Next End Sub

执行以上代码可以最大从0001份打印到9999份,并会在光标处自动加上打印份数编号。 如第10份 则会 在光标处插入打印份数编号0010.

也可通过修改以上代码实现更大数目的打印量:添加 并 修改相应的 if 语句。 以上宏代码的安装:

点击view视窗里面的宏按钮,选择 view macros 即会出现以上窗口, 输入 PrintCopies 做为 Macro name. 然后选择create 新建, 会出现一个代码框,将以上代码复制进去,点击保存就完成 PrintCopies 宏的安装了。

运行: 把光标放在希望插入打印份数编号的位置,设置好打印机,然后打开如上窗口,里面会有出现一个叫做 PrintCopies 的宏, 选上,然后单击运行,会出现2个框“Please enter the number of copies you want to print”和“Enter the starting number you want to print”。 分别输入相应的数值就好了。打印机就会自动按照你的设定和以上要求来完成打印。

方法二: 通过设置文档变量 来完成

思路:在WORD中添加一个文档变量, 用宏来进行打印,每打印一次就让文档变量自动更新,直到打印完毕。

这个过程里面也要用到宏,该方法是在 Microsoft Office Access 编程爱好者看到的,但里面的文档变量说的不是很清楚,基于本人又是word VBA的门外汉,所以就先按照简单的思路编写了自己的代码 救急(在上面作为方法一已经展示给大家了)。

后来有时间看了下word VBA help文件,按照此方法也成功实现了以上功能。现将该方法中的文档变量设置补充完成,并详细的展示在这里 通过宏来添加文档变量:

用上面提到的方法新建一个叫做AddDocumentVariablede 的宏。

Sub AddDocumentVariable()

ThisDocument.Variables.Add Name:=\End Sub

然后运行此宏 即完成文档变量PrintCount的添加。 再新建一个宏来完成自动打印并实现对文档变量PrintCount的调用。

Function SetPrintCount(ByVal CountNumber As String) Dim i As Long

Dim v As Variable

'Dim strVarName As String

Const strVarName As String = \ Dim strVarValue As String 'strVarName = \ strVarValue = CountNumber

Set v = ActiveDocument.Variables(strVarName) If v Is Nothing Then

Set v = ActiveDocument.Variables.Add(strVarName, strVarValue) Else

v.Value = strVarValue End If

For Each v In ActiveDocument.Variables

Debug.Print \ Next

Dim f As Field

For Each f In ActiveDocument.Fields

Debug.Print Left(f.Code.Text, Len(\strVarName))

If f.Kind = wdFieldKindWarm And Left(f.Code.Text, Len(\strVarName Then

'保留格式: \\* MERGEFORMAT 'Debug.Print f.Code.Text f.Update End If Next

End Function

Sub 生成份数编号并打印到当前打印机() Dim i As Long Dim lngStart Dim lngCount

lngCount = InputBox(\请输入您要打印的份数\\请输入您要打印的份数\

If lngCount = \ Exit Sub End If

lngStart = InputBox(\请输入您要打印的起始编号\\请输入您要打印的起始编号\

If lngStart = \ Exit Sub End If

For i = lngStart To lngCount

SetPrintCount Format(i, \

'由于测试系统中没有打印机,所以这里暂时输出到打印文件。如果要直接打印,可以直接用 Application.PrintOut 即可。

Application.PrintOut FileName:=\Range:=wdPrintAllDocument, Item:= _

wdPrintDocumentContent, Copies:=1, Pages:=\PageType:=wdPrintAllPages, _

ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _

False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _

PrintZoomPaperHeight:=0 Next End Sub

上面这个宏名为“生成份数编号并打印到当前打印机”,改来自Microsoft Office Access 编程爱好者的代码。另外可以修改 SetPrintCount Format(i, \中的 \来实现更大数目打印的扩展。然后在光标处插入域 “DOCVARIABLE PrintCount” 操作如下:

在insert插入栏中选择quick parts> field > Docvariable. 输入变量名 PrintCount。点击okay 即完成插入 文档变量PrintCount的操作。

然后运行“生成份数编号并打印到当前打印机” 的宏, 即可让WORD自动打印出打印份数。