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

更新时间:2023-06-06 17:08:28 阅读: 评论:0

如何让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("Plea enter the number of copies you want to print", "Plea enter the number of copies you want to print", 1)
    If lngCount = "" Then
        Exit Sub
    End If
    lngStart = InputBox("Enter the starting number you want to print", "Enter the starting number you want to print", 1)
酸奶可以加热喝吗    If lngStart = "" Then
        Exit Sub
    End If
    For i = lngStart To lngCount
    If i < 10 Then
    Selection.TypeText Text:="000" & i&
青涩日记    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=Fal, Collate:=True, Background:=True, PrintToFile:= _
        Fal, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    End If
黄金价格上涨 
    If (i >= 10) And (i < 100) Then
    Selection.TypeText Text:="00" & i&
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=Fal, Collate:=True, Background:=True, PrintToFile:= _
        Fal, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    End If
 
    If (i >= 100) And (i < 1000) Then
    Selection.TypeText Text:="0" & i&
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=Fal, Collate:=True, Background:=True, PrintToFile:= _
        Fal, 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:=Fal, Collate:=True, Background:=True, PrintToFile:= _
        Fal, 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个框“Plea 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:="PrintCount"
End Sub
然后运行此宏 即完成文档变量PrintCount的添加。 再新建一个宏来完成自动打印并实现对文档变量PrintCount的调用。boa
 
Function SetPrintCount(ByVal CountNumber As String)
    Dim i As Long
    Dim v As Variable
    'Dim strVarName As String保持音记号
    Const strVarName As String = "PrintCount"
中国古代民间故事
    Dim strVarValue As String
    'strVarName = "PrintCount"
    strVarValue = CountNumber
 
    Set v = ActiveDocument.Variables(strVarName)
    If v Is Nothing Then
        Set v = ActiveDocument.Variables.Add(strVarName, strVarValue)
    El
        v.Value = strVarValue
    End If
    For Each v In ActiveDocument.Variables
        Debug.Print "variables", v.Name, v.Value
    Next

本文发布于:2023-06-06 17:08:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/886324.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:打印   文档   变量   编号   份数   代码
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图