ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] DUPLEX-¦p¦ó±N¤U­z»yªkÀ³¥Î¦ÜVBA7

[µo°Ý] DUPLEX-¦p¦ó±N¤U­z»yªkÀ³¥Î¦ÜVBA7

  1. Option Explicit

  2. Private Sub Command1_Click()
  3. Dim MyWord As Object
  4. Set MyWord = CreateObject("Word.application")
  5. MyWord.Visible = True
  6. MyWord.documents.Open FileName:=CurDir & "\¤u¨ã²MÂI³æ.doc"
  7. SetPrinterDuplex Printer.DeviceName, 2
  8. MyWord.PrintOut
  9. MyWord.documents.Close 'FileName:=CurDir & "\¤u¨ã²MÂI³æ.doc"
  10. ' SetPrinterDuplex Printer.DeviceName, 1
  11. MyWord.documents.Open FileName:=CurDir & "\¶Ô«e·|ij.doc"
  12. SetPrinterDuplex Printer.DeviceName, 2
  13. MyWord.PrintOut
  14. MyWord.documents.Close
  15. MyWord.documents.Open FileName:=CurDir & "\¶Ô°È¤À°tªí.doc"
  16. SetPrinterDuplex Printer.DeviceName, 2
  17. MyWord.PrintOut
  18. MyWord.documents.Close
  19.       
  20.       
  21. MyWord.Quit
  22. Set MyWord = Nothing
  23. End Sub


  24. ¼Ò²Õ:

  25. Option Explicit

  26.    Public Type PRINTER_DEFAULTS

  27.        pDatatype As Long
  28.        pDevmode As Long
  29.        DesiredAccess As Long
  30.    End Type

  31.    Public Type PRINTER_INFO_2
  32.        pServerName As Long
  33.        pPrinterName As Long
  34.        pShareName As Long
  35.        pPortName As Long
  36.        pDriverName As Long
  37.        pComment As Long
  38.        pLocation As Long
  39.        pDevmode As Long       ' Pointer to DEVMODE
  40.        pSepFile As Long
  41.        pPrintProcessor As Long
  42.        pDatatype As Long
  43.        pParameters As Long
  44.        pSecurityDescriptor As Long  ' Pointer to SECURITY_DESCRIPTOR
  45.        Attributes As Long


  46.        Priority As Long
  47.        DefaultPriority As Long
  48.        StartTime As Long
  49.        UntilTime As Long
  50.        Status As Long
  51.        cJobs As Long
  52.        AveragePPM As Long
  53.    End Type

  54.    Public Type DEVMODE
  55.        dmDeviceName As String * 32

  56.        dmSpecVersion As Integer
  57.        dmDriverVersion As Integer
  58.        dmSize As Integer
  59.        dmDriverExtra As Integer
  60.        dmFields As Long
  61.        dmOrientation As Integer
  62.        dmPaperSize As Integer
  63.        dmPaperLength As Integer
  64.        dmPaperWidth As Integer
  65.        dmScale As Integer
  66.        dmCopies As Integer
  67.        dmDefaultSource As Integer
  68.        dmPrintQuality As Integer
  69.        dmColor As Integer
  70.        dmDuplex As Integer
  71.        dmYResolution As Integer
  72.        dmTTOption As Integer
  73.        dmCollate As Integer
  74.        dmFormName As String * 32
  75.        dmUnusedPadding As Integer
  76.        dmBitsPerPel As Integer
  77.        dmPelsWidth As Long
  78.        dmPelsHeight As Long
  79.        dmDisplayFlags As Long
  80.        dmDisplayFrequency As Long
  81.        dmICMMethod As Long
  82.        dmICMIntent As Long
  83.        dmMediaType As Long
  84.        dmDitherType As Long
  85.        dmReserved1 As Long
  86.        dmReserved2 As Long
  87.    End Type

  88.    Public Const DM_DUPLEX = &H1000&
  89.    Public Const DM_IN_BUFFER = 8

  90.    Public Const DM_OUT_BUFFER = 2
  91.    Public Const PRINTER_ACCESS_ADMINISTER = &H4
  92.    Public Const PRINTER_ACCESS_USE = &H8
  93.    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
  94.    Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
  95.              PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

  96.    Public Declare Function ClosePrinter Lib "winspool.drv" _
  97.     (ByVal hPrinter As Long) As Long
  98.    Public Declare Function DocumentProperties Lib "winspool.drv" _
  99.      Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
  100.      ByVal hPrinter As Long, ByVal pDeviceName As String, _
  101.      ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, _
  102.      ByVal fMode As Long) As Long
  103.    Public Declare Function GetPrinter Lib "winspool.drv" Alias _
  104.      "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
  105.      pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
  106.    Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
  107.      "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
  108.      pDefault As PRINTER_DEFAULTS) As Long
  109.    Public Declare Function SetPrinter Lib "winspool.drv" Alias _
  110.      "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
  111.      pPrinter As Byte, ByVal Command As Long) As Long

  112.    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  113.     (pDest As Any, pSource As Any, ByVal cbLength As Long)

  114.    ' ==================================================================
  115.    ' SetPrinterDuplex.
  116.    '
  117.    '  Programmatically set the Duplex flag for the specified default properties
  118.    '  of the printer driver.
  119.    '
  120.    '  Returns: True on success and False on error. An error also

  121.    '  displays a message box. This message box is displayed for information
  122.    '  only. You must modify the code to support better error
  123.    '  handling in your production application.
  124.    '
  125.    '  Parameters:
  126.    '    sPrinterName - The name of the printer to be used.
  127.    '
  128.    '    nDuplexSetting - One of the following standard settings:
  129.    '       1 = None
  130.    '       2 = Duplex on long edge (book)
  131.    '       3 = Duplex on short edge (legal)
  132.    '
  133.    ' ==================================================================
  134.    Public Function SetPrinterDuplex(ByVal sPrinterName As String, _
  135.    ByVal nDuplexSetting As Long) As Boolean

  136.       Dim hPrinter As Long
  137.       Dim pd As PRINTER_DEFAULTS
  138.       Dim pinfo As PRINTER_INFO_2
  139.       Dim dm As DEVMODE
  140.    
  141.       Dim yDevModeData() As Byte
  142.       Dim yPInfoMemory() As Byte
  143.       Dim nBytesNeeded As Long
  144.       Dim nRet As Long, nJunk As Long
  145.    
  146.       On Error GoTo cleanup
  147.    
  148.       If (nDuplexSetting < 1) Or (nDuplexSetting > 3) Then
  149.          MsgBox "Error: dwDuplexSetting is incorrect."
  150.          Exit Function
  151.       End If
  152.       
  153.       pd.DesiredAccess = PRINTER_ALL_ACCESS
  154.       nRet = OpenPrinter(sPrinterName, hPrinter, pd)
  155.       If (nRet = 0) Or (hPrinter = 0) Then
  156.          If Err.LastDllError = 5 Then
  157.             MsgBox "Access denied -- See the article for more info."
  158.          Else
  159.             MsgBox "Cannot open the printer specified " & _
  160.               "(make sure the printer name is correct)."
  161.          End If
  162.          Exit Function
  163.       End If
  164.    
  165.       nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
  166.       If (nRet < 0) Then
  167.          MsgBox "Cannot get the size of the DEVMODE structure."
  168.          GoTo cleanup
  169.       End If
  170.    
  171.       ReDim yDevModeData(nRet + 100) As Byte
  172.       nRet = DocumentProperties(0, hPrinter, sPrinterName, _
  173.                   VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
  174.       If (nRet < 0) Then
  175.          MsgBox "Cannot get the DEVMODE structure."
  176.          GoTo cleanup
  177.       End If
  178.    
  179.       Call CopyMemory(dm, yDevModeData(0), Len(dm))
  180.    
  181.       If Not CBool(dm.dmFields And DM_DUPLEX) Then
  182.         MsgBox "You cannot modify the duplex flag for this printer " & _
  183.                "because it does not support duplex or the driver " & _
  184.                "does not support setting it from the Windows API."
  185.          GoTo cleanup
  186.       End If
  187.    
  188.       dm.dmDuplex = nDuplexSetting
  189.       Call CopyMemory(yDevModeData(0), dm, Len(dm))
  190.    
  191.       nRet = DocumentProperties(0, hPrinter, sPrinterName, _
  192.         VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
  193.         DM_IN_BUFFER Or DM_OUT_BUFFER)

  194.       If (nRet < 0) Then
  195.         MsgBox "Unable to set duplex setting to this printer."
  196.         GoTo cleanup
  197.       End If
  198.    
  199.       Call GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)
  200.       If (nBytesNeeded = 0) Then GoTo cleanup
  201.    
  202.       ReDim yPInfoMemory(nBytesNeeded + 100) As Byte

  203.       nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)
  204.       If (nRet = 0) Then
  205.          MsgBox "Unable to get shared printer settings."
  206.          GoTo cleanup
  207.       End If
  208.    
  209.       Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))
  210.       pinfo.pDevmode = VarPtr(yDevModeData(0))
  211.       pinfo.pSecurityDescriptor = 0
  212.       Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))
  213.    
  214.       nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
  215.       If (nRet = 0) Then
  216.          MsgBox "Unable to set shared printer settings."
  217.       End If
  218.    
  219.       SetPrinterDuplex = CBool(nRet)

  220. cleanup:
  221.       If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)

  222.    End Function
½Æ»s¥N½X
hello §A¦n¶Ü

        ÀR«ä¦Û¦b : ·R¤£¬O­n¨D¹ï¤è¡A¦Ó¬O­n¥Ñ¦Û¨­ªº¥I¥X¡C
ªð¦^¦Cªí ¤W¤@¥DÃD