RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1192385
Accepted
Alexander Khudyakov
Alexander Khudyakov
Asked:2021-10-19 20:22:37 +0000 UTC2021-10-19 20:22:37 +0000 UTC 2021-10-19 20:22:37 +0000 UTC

在具有不同设置的打印机上打印文档的脚本使用什么?

  • 772

实际上,我在工作中收到了很多 docx 文档,我必须根据页数使用不同的打印机设置进行打印。如果是2,则双面打印。如果超过 4 个,则每张纸放置两页并在两面打印。这通常是,但并非总是如此。

我决定有必要自动化这个过程,但我不知道在哪里挖掘。什么值得使用?Python?C#?爪哇?蝙蝠脚本?要包括哪些库?或者也许有一些针对这种情况的程序?接口不重要,可以通过控制台。

请给点建议。我会很感激。

любой-язык
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Alexander Khudyakov
    2021-10-20T19:15:46Z2021-10-20T19:15:46Z

    通过一些谷歌搜索,我发现解决我的问题的最简单方法是使用Visual Basic Applications。

    我会为那些正在寻找类似东西的人留下一些线索。

    借助这个片段,我们可以统计当前打开的 Word 文档的页数:

    Dim NumPages As Long
    NumPages = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
    

    打印文档很容易,该行将对此有所帮助:

    ActiveDocument.PrintOut Background:=True, Copies:=5, Collate:=True, PrintZoomColumn:=2, PrintZoomRow:=1 '5 копий будет напечатано
    

    事实证明双面打印更加困难,因为 VBA 无法访问打印机设置,但以下代码可以帮助我们:

     ' Due to 64-bit version some pointers were changed into LogtPtr type of data
     
     Option Explicit
    
       Public Type PRINTER_DEFAULTS
    
           pDatatype As LongPtr
           pDevmode As LongPtr
           DesiredAccess As LongPtr
       End Type
    
       Public Type PRINTER_INFO_2
           pServerName As LongPtr
           pPrinterName As LongPtr
           pShareName As LongPtr
           pPortName As LongPtr
           pDriverName As LongPtr
           pComment As LongPtr
           pLocation As LongPtr
           pDevmode As LongPtr       ' Pointer to DEVMODE
           pSepFile As LongPtr
           pPrintProcessor As LongPtr
           pDatatype As LongPtr
           pParameters As LongPtr
           pSecurityDescriptor As LongPtr  ' Pointer to SECURITY_DESCRIPTOR
           Attributes As LongPtr
    
    
           Priority As Long
           DefaultPriority As Long
           StartTime As Long
           UntilTime As Long
           Status As Long
           cJobs As Long
           AveragePPM As Long
       End Type
    
       Public Type DEVMODE
           dmDeviceName As String * 32
    
           dmSpecVersion As Integer
           dmDriverVersion As Integer
           dmSize As Integer
           dmDriverExtra As Integer
           dmFields As Long
           dmOrientation As Integer
           dmPaperSize As Integer
           dmPaperLength As Integer
           dmPaperWidth As Integer
           dmScale As Integer
           dmCopies As Integer
           dmDefaultSource As Integer
           dmPrintQuality As Integer
           dmColor As Integer
           dmDuplex As Integer
           dmYResolution As Integer
           dmTTOption As Integer
           dmCollate As Integer
           dmFormName As String * 32
           dmUnusedPadding As Integer
           dmBitsPerPel As Integer
           dmPelsWidth As Long
           dmPelsHeight As Long
           dmDisplayFlags As Long
           dmDisplayFrequency As Long
           dmICMMethod As Long
           dmICMIntent As Long
           dmMediaType As Long
           dmDitherType As Long
           dmReserved1 As Long
           dmReserved2 As Long
       End Type
    
       Public Const DM_DUPLEX = &H1000&
       Public Const DM_IN_BUFFER = 8
    
       Public Const DM_OUT_BUFFER = 2
       Public Const PRINTER_ACCESS_ADMINISTER = &H4
       Public Const PRINTER_ACCESS_USE = &H8
       Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
       Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
                 PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
                 
        Public Declare PtrSafe Function ClosePrinter Lib "winspool.drv" _
        (ByVal hPrinter As LongPtr) As Long
       Public Declare PtrSafe Function DocumentProperties Lib "winspool.drv" _
         Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
         ByVal hPrinter As LongPtr, ByVal pDeviceName As String, _
         ByVal pDevModeOutput As LongPtr, ByVal pDevModeInput As LongPtr, _
         ByVal fMode As LongPtr) As Long
       Public Declare PtrSafe Function GetPrinter Lib "winspool.drv" Alias _
         "GetPrinterA" (ByVal hPrinter As LongPtr, ByVal Level As Long, _
         pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
       Public Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias _
         "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As LongPtr, _
         pDefault As PRINTER_DEFAULTS) As Long
       Public Declare PtrSafe Function SetPrinter Lib "winspool.drv" Alias _
         "SetPrinterA" (ByVal hPrinter As LongPtr, ByVal Level As Long, _
         pPrinter As Byte, ByVal Command As Long) As Long
    
       Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (pDest As Any, pSource As Any, ByVal cbLength As LongPtr)
     
    
     
     Public Function SetPrinterDuplex(ByVal sPrinterName As String, _
       ByVal nDuplexSetting As Long) As Boolean
    
          Dim hPrinter As LongPtr
          Dim pd As PRINTER_DEFAULTS
          Dim pinfo As PRINTER_INFO_2
          Dim dm As DEVMODE
       
          Dim yDevModeData() As Byte
          Dim yPInfoMemory() As Byte
          Dim nBytesNeeded As Long
          Dim nRet As Long, nJunk As Long
       
          On Error GoTo cleanup
       
          If (nDuplexSetting < 1) Or (nDuplexSetting > 3) Then
             MsgBox "Error: dwDuplexSetting is incorrect."
             Exit Function
          End If
          
          pd.DesiredAccess = PRINTER_ALL_ACCESS
          nRet = OpenPrinter(sPrinterName, hPrinter, pd)
          If (nRet = 0) Or (hPrinter = 0) Then
             If Err.LastDllError = 5 Then
                MsgBox "Access denied -- See the article for more info."
             Else
                MsgBox "Cannot open the printer specified " & _
                  "(make sure the printer name is correct)."
             End If
             Exit Function
          End If
       
          nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
          If (nRet < 0) Then
             MsgBox "Cannot get the size of the DEVMODE structure."
             GoTo cleanup
          End If
       
          ReDim yDevModeData(nRet + 100) As Byte
          nRet = DocumentProperties(0, hPrinter, sPrinterName, _
                      VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
          If (nRet < 0) Then
             MsgBox "Cannot get the DEVMODE structure."
             GoTo cleanup
          End If
       
          Call CopyMemory(dm, yDevModeData(0), Len(dm))
       
          If Not CBool(dm.dmFields And DM_DUPLEX) Then
            MsgBox "You cannot modify the duplex flag for this printer " & _
                   "because it does not support duplex or the driver " & _
                   "does not support setting it from the Windows API."
             GoTo cleanup
          End If
       
          dm.dmDuplex = nDuplexSetting
          Call CopyMemory(yDevModeData(0), dm, Len(dm))
       
          nRet = DocumentProperties(0, hPrinter, sPrinterName, _
            VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
            DM_IN_BUFFER Or DM_OUT_BUFFER)
    
          If (nRet < 0) Then
            MsgBox "Unable to set duplex setting to this printer."
            GoTo cleanup
          End If
       
          Call GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)
          If (nBytesNeeded = 0) Then GoTo cleanup
       
          ReDim yPInfoMemory(nBytesNeeded + 100) As Byte
    
          nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)
          If (nRet = 0) Then
             MsgBox "Unable to get shared printer settings."
             GoTo cleanup
          End If
       
          Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))
          pinfo.pDevmode = VarPtr(yDevModeData(0))
          pinfo.pSecurityDescriptor = 0
          Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))
       
          nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
          If (nRet = 0) Then
             MsgBox "Unable to set shared printer settings."
          End If
       
          SetPrinterDuplex = CBool(nRet)
    
    cleanup:
          If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
    
       End Function
    

    现在我们可以在使用以下代码段打印时使用打印机:

    ActivePrinter = "ПИШЕМ СЮДА НАЗВАНИЕ ПРИНТЕРА"
    Call SetPrinterDuplex(ActivePrinter, 2) '2 - печать с отражением по длинной стороне, 3 - печать по короткой стороне
    ActiveDocument.PrintOut Background:=True, Copies:=NumbersArray(i), Collate:=True, PrintZoomColumn:=1, PrintZoomRow:=1
    

    使用 PrintZoomColumn 参数,我们可以设置列数,使用 PrintZoomRow 我们可以设置行数。Collat​​e 参数要求将当前副本打印到最后,这是我们使用多个副本的时候。

    剩下的只是添加脚本逻辑和自动化准备就绪。

    类似的东西。

    • 2

相关问题

  • 在某些情况下不要执行代码[关闭]

  • 脚本叠加

  • Code Golf:Snake 2D 数组遍历

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5