• 重复打印相同内容(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
  • 相关阅读:
    .Net下RabbitMQ的使用(1) 初识RabbitMQ
    Android GridView用法,用到了BaseAdapter
    android 代码布局简单的例子
    ActivityGroup的简单用法(1)详细讲解
    devc++中编译含WINSOCK的代码出现错误的解决方法
    Qt源码分析之QPointer
    QML基础——初识Qt Quick Designer
    Qt源码分析之信号和槽机制
    QML基础——UI布局管理
    Qt源码分析之QObject
  • 原文地址:https://www.cnblogs.com/dimg/p/7574003.html
Copyright © 2020-2023  润新知