HOWTO: 設定 Word 自動化的雙面列印

文章翻譯 文章翻譯
文章編號: 230743 - 檢視此文章適用的產品。
全部展開 | 全部摺疊

在此頁中

結論

Windows 的 Microsoft Word 並不提供自動化用戶端開始列印工作之前,先設定雙面列印旗標的方法。雖然指示支援雙面列印 PrintOut 方法中有一個參數,參數並不提供,則為 True 的雙面列印,並可能無法使用您的作業系統而定或安裝的語言。 不過,開發人員可以解決藉由呼叫 Word 的 PrintOut 函式之前先變更雙工旗標為作用中的印表機驅動程式在 Windows 系統上這種限制。

本文將告訴您,如何使用 Windows API 來變更作用中印表機的雙面列印設定,並允許工要列印的 Word 文件。

其他相關資訊

這個程式碼使用 DocumentProperties API 來變更列印設定的印表機驅動程式來啟用雙面列印。這個程式碼已順利運作一般使用者必須變更印表機的通用的列印設定的適當權限。如果使用者沒有變更驅動程式設定適當的權限,它們將會得到拒絕存取 」 錯誤上 OpenPrinter API 呼叫。

Microsoft Windows NT 與 Microsoft Windows 2000 給需要列印到共用的網路印表機上的使用者,這可能會發生問題,因為列印驅動程式不在本機電腦上,但在列印伺服器上。雖然可以讓系統管理員來設定列印伺服器,讓結束使用者以變更全域設定適當的權限,不希望在大部分的情況中這麼做。如果要解決這個問題,有可能安裝本機列印驅動程式的網路的印表機,並讓每位使用者控制他們的本機系統的設定。

步驟,在 Windows NT 和 Windows 2000 上新增網路印表機的本機列印驅動程式

  1. 一個在 [開始] 功能表選取 [設定],然後選取 印表機,連按兩下 [新增印表機],以帶出 「 新增印表機精靈 」。
  2. 出現提示時, 選取 [從 我的電腦 」 及 NOT 網路上的印表機]。雖然您會連線到網路印表機,您想要使用 [我的電腦上的 [驅動程式。請按 [下一步] 以繼續。
  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 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
關鍵字:?
kbmt kbapi kbautomation kbhowto kbprint KB230743 KbMtzh
機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本: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