+ Reply to Thread
Results 1 to 5 of 5

VBA print problem

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2010
    Location
    brussels
    MS-Off Ver
    Excel 2003
    Posts
    63

    VBA print problem

    Hello,

    Following code I use to print out my page on sheet 1. Now I have also a page on sheet 2 i want to print on the back.
    Can anyone help me to change the code please, i don't know exactly how to do this.

    Sub Printen()
        Dim SelecteerPrinter As Variant
        SelecteerPrinter = Application.Dialogs(xlDialogPrinterSetup).Show
        ActiveSheet.PrintOut Copies:=2, ActivePrinter:=SelecteerPrinter, Collate:=True
    End Sub
    Many thanks
    Aquila
    Last edited by Aquila; 03-31-2010 at 10:18 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA print problem

    Hello Aquila,

    Neither Excel nor VBA has a built-in function to set your printer to "duplex" mode. Sometime ago I looked into how to do this. It requires using the Windows API. Copy all this code into a separate VBA module.
       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, _
              ByRef pPrinter As Byte, _
              ByVal cbBuf As Long, _
              ByRef pcbNeeded As Long) As Long
         
       Public Declare Function OpenPrinter _
         Lib "winspool.drv" _
           Alias "OpenPrinterA" _
             (ByVal pPrinterName As String, _
              ByRef phPrinter As Long, _
              ByRef pDefault As PRINTER_DEFAULTS) As Long
         
       Public Declare Function SetPrinter _
         Lib "winspool.drv" _
         Alias "SetPrinterA" _
           (ByVal hPrinter As Long, _
             ByVal Level As Long, _
             ByRef pPrinter As Byte, _
             ByVal Command As Long) As Long
    
       Public Declare Sub CopyMemory _
         Lib "kernel32" _
           Alias "RtlMoveMemory" _
             (ByRef pDest As Any, _
              ByRef pSource As Any, _
              ByVal cbLength As Long)
     
       Private Declare Function StrLen _
         Lib "kernel32" _
           Alias "lstrlenA" _
             (ByVal lpString As Long) As Long
     
       ' ==================================================================
       ' SetPrinterToDuplex.
       '
       '  Set the Duplex flag for the specified default properties
       '  of the printer driver.
       '
       '  Returns: True on success and False on error. An error will also
       '  display a message box. These messages are displayed for information
       '  only.
       '
       '  Parameters:
       '    PrinterName - The name of the printer to be used.
       '
       '    DuplexSetting - One of the following standard settings:
       '       1 = None
       '       2 = Duplex on long edge (book)
       '       3 = Duplex on short edge (legal)
       '
       ' ==================================================================
       Public Function SetPrinterToDuplex(ByVal PrinterName As String, _
                                          ByVal DuplexSetting As Long) As Boolean
    
          Dim hPrinter As Long
          Dim PD As PRINTER_DEFAULTS
          Dim PINFO As PRINTER_INFO_2
          Dim DM As DEVMODE
       
          Dim DevModeData() As Byte
          Dim PInfoMemory() As Byte
          Dim nBytesNeeded As Long
          Dim nRet As Long, nJunk As Long
       
          On Error GoTo cleanup
       
          If (DuplexSetting < 1) Or (DuplexSetting > 3) Then
             MsgBox "Error: dwDuplexSetting is incorrect."
             Exit Function
          End If
          
          PD.DesiredAccess = PRINTER_ALL_ACCESS
          nRet = OpenPrinter(PrinterName, 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, PrinterName, 0, 0, 0)
          If (nRet < 0) Then
             MsgBox "Cannot get the size of the DEVMODE structure."
             GoTo cleanup
          End If
       
          ReDim DevModeData(nRet + 100) As Byte
          nRet = DocumentProperties(0, hPrinter, PrinterName, _
                      VarPtr(DevModeData(0)), 0, DM_OUT_BUFFER)
          If (nRet < 0) Then
             MsgBox "Cannot get the DEVMODE structure."
             GoTo cleanup
          End If
       
          Call CopyMemory(DM, DevModeData(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 = DuplexSetting
          Call CopyMemory(DevModeData(0), DM, Len(DM))
       
          nRet = DocumentProperties(0, hPrinter, PrinterName, _
            VarPtr(DevModeData(0)), VarPtr(DevModeData(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 PInfoMemory(nBytesNeeded + 100) As Byte
    
          nRet = GetPrinter(hPrinter, 2, PInfoMemory(0), nBytesNeeded, nJunk)
          If (nRet = 0) Then
             MsgBox "Unable to get shared printer settings."
             GoTo cleanup
          End If
       
          Call CopyMemory(PINFO, PInfoMemory(0), Len(PINFO))
          PINFO.pDevmode = VarPtr(DevModeData(0))
          PINFO.pSecurityDescriptor = 0
          Call CopyMemory(PInfoMemory(0), PINFO, Len(PINFO))
       
          nRet = SetPrinter(hPrinter, 2, PInfoMemory(0), 0)
          If (nRet = 0) Then
             MsgBox "Unable to set shared printer settings."
          End If
       
          SetPrinterToDuplex = CBool(nRet)
    
    cleanup:
          If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
    
       End Function


    Using the Macro
      '2 sets the duplex mode to book style, 1 sets the printer back to a single page
      SetPrinterToDuplex "Canon MX300 series Printer", 2
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-09-2010
    Location
    brussels
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: VBA print problem

    Thankx Leith for fast reply!

    It's clear know, i could search a long time.


  4. #4
    Registered User
    Join Date
    12-23-2008
    Location
    Chennai,India,Tamilnadu
    Posts
    70

    Question Re: VBA print problem

    Dear Friend,

    Please help me to write VBA code for setting PDF creator as a default printer and PDF creation through that.


    Regards
    M.Velmurugan

  5. #5
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: VBA print problem

    Hi there,
    @ Leith Ross, I think your code is great!
    I tried your code at work but I have some problems to access the printers.
    We're on a LAN network and all of our printer have a static IP, I have policies to access all of them since they're in the root of our server. I run the pc as Local Administrator.
    I can access and print in all of the printers, but as soon as I run the SetPrinterToDuplex function I get a "Access denied - see the article for more info" message.
    Please, can you help me in the process to implement this very useful function?

    thanks in advance
    P.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1