重复打印相同内容(Doc档)的时候自动生成打印编号

昨天突然接到一个好久未联系的朋友电话,说是江湖救急,要打印一份单据,单据上有个号码要自动生成,如下图,最土的办法是打印完一张,手工改下号码,但这种方法估计碰到成百上千张时估计会疯掉

 

网上找了实现方法,用Office中的宏即可解决,我把宏里面的代码依此实际需求做了些调整并贴出来,关键代码做下注释,以方便有同样需要的人使用

 

Sub PrintCopies()

    Dim i As Long
    Dim lngStart
    Dim lngCount
    Dim rngDoc As Range
    
    '提示需要打印多少份
    lngCount = InputBox("Please enter the number of copies you want to print", "Please enter the number of copies you want to print", 1)
    
    If lngCount = "" Then
        Exit Sub
    End If
    
    '提示本次打印从哪个号码开始打印,默认为1
    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
    
        '编号的位置,依实际状况不同,需要修改Start和End的值
        Set rngDoc = ActiveDocument.Range(Start:=22, End:=25)
        
        '数字不满3位时前面补0
        If i < 10 Then
            rngDoc.Text = "00" & i
        End If
        
        If (i >= 10) And (i < 100) Then
            rngDoc.Text = "0" & i
        End If
        
        If (i >= 100) And (i < 1000) Then
            rngDoc.Text = i
        End If
        
        '调用打印
        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
        
        '未使用此方法,相当于在当前选择项上按Backspace键
        'Selection.TypeBackspace
    Next
End Sub

 

posted @ 2017-09-22 11:33  大海胸懷  阅读(1315)  评论(0编辑  收藏  举报