HOWTO: 设置 Word 自动打印双面打印

文章翻译 文章翻译
文章编号: 230743 - 查看本文应用于的产品
展开全部 | 关闭全部

本文内容

概要

对于 Windows 的 Microsoft Word 不提供自动化客户端启动打印作业之前设置双面打印标志的方法。尽管指示支持双面打印的 PrintOut 方法中有一个参数参数不提供真正的双面打印,并可能不可用,这取决于您的操作系统或安装的语言。 但是,开发人员可以变通解决此限制在 Windows 系统通过调用 Word 的 PrintOut 函数之前更改活动的打印机驱动程序双面打印的标志。

本文演示了如何使用 Windows API 更改活动打印机的双面打印设置并允许在双面打印中打印的 Word 文档。

更多信息

此代码使用 DocumentProperties API 更改打印机驱动程序的打印设置来启用双面打印。对于成功处理的该代码最终用户将需要足够的权限,若要更改打印机的打印全局设置。如果用户不具有更改驱动程序设置适当的权限,他们将得到一个访问被拒绝错误 OpenPrinter API 调用时。

对于 Microsoft Windows NT 和 Microsoft Windows 2000 需要打印到共享的网络打印机的用户这可以是一个问题,因为打印驱动程序未驻留在本地计算机上,但在打印服务器上。尽管管理员可以将打印服务器配置为最终用户授予适当的权限更改全局设置的可能它是不希望在大多数情况下执行此操作。要变通解决此问题,就可以安装打印本地网络的打印机驱动程序,并让每个用户控制其本地系统的设置。

添加本地的打印驱动程序的 Windows NT 和 Windows 2000 上的网络打印机的步骤

  1. 一个 开始 菜单选择 设置,然后选择 打印机,双击 $ 添加打印机 以显示添加打印机向导。
  2. 当系统提示,选择从"我的电脑"并不从网络的打印机。尽管您将连接到网络打印机,但您希望在我的计算机上使用的驱动程序。请按 下一步 继续。
  3. 单击上"添加端口
  4. 在下拉列表框中选择 本地端口,然后单击 新端口
  5. 键入在网络上的打印机的位置。例如:
        \\printserver\printername (using the exact path name to the printer)
    					
  6. 选择 确定,继续执行安装程序的其余部分。
更改活动打印机的打印机属性将影响所有使用该打印机,和而不只是 Word 的应用程序。如果打算更改特定的打印作业的设置,确保在作业完成时还原设置。

生成示例的步骤

  1. 打开 Visual Basic 并创建一个新的项目。默认情况下创建 Form1。
  2. 将标准 BAS 模块添加到项目并将下面的代码添加到模块的代码窗口:
      
     Option Explicit
    
       Public Type PRINTER_DEFAULTS
    
           pDatatype As Long
           pDevmode As Long
           DesiredAccess As Long
       End Type
    
       Public Type PRINTER_INFO_2
           pServerName As Long
           pPrinterName As Long
           pShareName As Long
           pPortName As Long
           pDriverName As Long
           pComment As Long
           pLocation As Long
           pDevmode As Long       ' Pointer to DEVMODE
           pSepFile As Long
           pPrintProcessor As Long
           pDatatype As Long
           pParameters As Long
           pSecurityDescriptor As Long  ' Pointer to SECURITY_DESCRIPTOR
           Attributes As Long
    
    
           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 Function ClosePrinter Lib "winspool.drv" _
        (ByVal hPrinter As Long) As Long
       Public Declare Function DocumentProperties Lib "winspool.drv" _
         Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
         ByVal hPrinter As Long, ByVal pDeviceName As String, _
         ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, _
         ByVal fMode As Long) As Long
       Public Declare Function GetPrinter Lib "winspool.drv" Alias _
         "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
         pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
       Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
         "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
         pDefault As PRINTER_DEFAULTS) As Long
       Public Declare Function SetPrinter Lib "winspool.drv" Alias _
         "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
         pPrinter As Byte, ByVal Command As Long) As Long
    
       Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (pDest As Any, pSource As Any, ByVal cbLength As Long)
     
       ' ==================================================================
       ' SetPrinterDuplex 
       '
       '  Programmatically set the Duplex flag for the specified printer
       '  driver's default properties.
       '
       '  Returns: True on success, False on error. (An error will also
    
       '  display a message box. This is done for informational value
       '  only. You should modify the code to support better error
       '  handling in your production application.)
       '
       '  Parameters:
       '    sPrinterName - The name of the printer to be used.
       '
       '    nDuplexSetting - One of the following standard settings:
       '       1 = None
       '       2 = Duplex on long edge (book)
       '       3 = Duplex on short edge (legal)
       '
       ' ==================================================================
       Public Function SetPrinterDuplex(ByVal sPrinterName As String, _
    
           ByVal nDuplexSetting As Long) As Boolean
    
          Dim hPrinter As Long
          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
    
    
    					
  3. 在 Form1,上添加一个标准的命令按钮。
  4. form1 将下面的代码添加到代码窗口:
       Option Explicit
       
       Private Sub Command1_Click()
          Dim oWord As Object
          Dim oDoc As Object
          
          Set oWord = CreateObject("Word.application")
    
          oWord.Visible = True
          
          Set oDoc = oWord.Documents.Add
          oDoc.Range.Select
          
    
          oWord.Selection.TypeText "This is on page 1" & vbCr
          oWord.Selection.InsertBreak 1
          oWord.Selection.TypeText "This is page 2"
          
          SetPrinterDuplex Printer.DeviceName, 2
          
          oDoc.PrintOut Background:=False
          
          SetPrinterDuplex Printer.DeviceName, 1
          
          MsgBox "Print Done", vbMsgBoxSetForeground
          
          oDoc.Saved = True
          oDoc.Close
          Set oDoc = Nothing
       
          oWord.Quit
          Set oWord = Nothing
       End Sub
    
    					
  5. 运行此示例。如果打印机支持双面打印的页的两面应打印测试文档。

参考

有关问题的详细信息,您可能会看到试图打印 Word 文档用的是双面打印机,请参阅下面 Microsoft 知识库中相应的文章:

176189WD97: 的实心黑色或灰色底纹不打印双面打印在 NT 上

196857WD97: 第一页打印上与双面打印的最后一页的背面

214683WD97: 双面打印不使用页面上的对象不双面

属性

文章编号: 230743 - 最后修改: 2007年1月24日 - 修订: 2.3
这篇文章中的信息适用于:
  • Microsoft Visual Basic 5.0 专业版
  • Microsoft Visual Basic 6.0 专业版
  • Microsoft Visual Basic 5.0 企业版
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Word 2002 标准版
  • Microsoft Word 2000 标准版
  • Microsoft Word 97 标准版
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
关键字:?
kbmt kbapi kbautomation kbhowto kbprint KB230743 KbMtzh
机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 230743
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。

提供反馈

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com