VBA : où trouver les commandes (options imprimane)

Résolu
Bonjour,
Une de mes imprimantes (Canon MG6350) a une fonction Recto-Verso. Une seconde n'a pas cette fonction.
Comment connaître les commandes de chacune pour agir via VBA, en évitant la boîte de dialogue, c'est à dire en utilisant les instructions telles ces exemples :
Feuil1.PageSetup.Orientation = xlLandsxcape
Feuil1.PageSetup.CenterVertically = True
ect...
Où trouver ces informations ?
Merci d'avance

--
bab

8 réponses

  • Votes
  • Date
  • Date décroissante
  1. Bonjour.
    Effectivement, j'ai bien eu ta réponse.
    Le traitement de cette dernière ne me satisfait pas.
    J'aimerais te soumettre le classeur réalisé pour mes essais, car je n'arrive toujours pas à forcer cette option.
    Même quand je met manuellement l'option via le dialogue du pilote, la lecture des propriétés me le désigne comme Faux.
    De plus, je ne vois pas comment le mettre Faux, s'il était affiché Vrai.
    J'ai tardé à répondre, des petits problèmes perso m'ont éloigné de mon PC.
    A bientôt, si tu veux bien accepter de vérifier mon classeur.
    1. Contributeur
      Voici le code pour détecter une imprimante recto verso en fonction du précédent:

      Sub Proprietesrectoverso()
      Dim objWMIService As Object, colItems As Object
      Dim objItem As Object
      Dim strComputer As String
      Dim i As Byte
      Dim etat As Boolean
      On Error Resume Next
      strComputer = "."
       
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.execQuery("Select * from Win32_printerConfiguration", , 48)
       
      For Each objItem In colItems
      etat = objItem.Duplex 'rectoverso
      If etat = True Then
      'action
      SetPrinterToDuplex 'on imprime en recto verso
      MsgBox "L'imprimante " & objItem.Name & " est équipée de recto verso"
      Else
      MsgBox "L'imprimante " & objItem.Name & " n'a pas de recto verso"
      End If
      Next
      End Sub


      1. Merci.
        Tu as bien fait de trouver la correspondance Duplex ==> Recto-Verso.
        Je me demande encore la signification des autres critères, toutefois inutiles pour mon besoin actuel.
        A + peut-être, Bab
    2. Contributeur
      Je viens de m'apercevoir que dans ce lien que je t'ai donné, tu as la propriété Duplex qui est le Recto Verso

      https://excel.developpez.com/faq/?page=Impression#ProprietesImprimantes

      Sub ProprietesImprimantes()
      Dim objWMIService As Object, colItems As Object
      Dim objItem As Object
      Dim strComputer As String
      Dim i As Byte
       
      On Error Resume Next
      strComputer = "."
       
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.execQuery("Select * from Win32_printerConfiguration", , 48)
       
      For Each objItem In colItems
          i = i + 1
          Cells(1, i) = "bitsPerPel: " & objItem.bitsPerPel
          Cells(2, i) = "Caption: " & objItem.Caption
          Cells(3, i) = "Collate: " & objItem.Collate
          Cells(4, i) = "Color: " & objItem.Color
          Cells(5, i) = "Copies: " & objItem.Copies
          Cells(6, i) = "Description: " & objItem.Description
          Cells(7, i) = "deviceName: " & objItem.deviceName
          Cells(8, i) = "displayFlags: " & objItem.displayFlags
          Cells(9, i) = "displayFrequency: " & objItem.displayFrequency
          Cells(10, i) = "ditherType: " & objItem.ditherType
          Cells(11, i) = "driverVersion: " & objItem.driverVersion
          Cells(12, i) = "Duplex: " & objItem.Duplex 'rectoverso
          Cells(13, i) = "formName: " & objItem.formName
          Cells(14, i) = "horizontalResolution: " & objItem.horizontalResolution
          Cells(15, i) = "ICMIntent: " & objItem.ICMIntent
          Cells(16, i) = "ICMMethod: " & objItem.ICMMethod
          Cells(17, i) = "logPixels: " & objItem.logPixels
          Cells(18, i) = "mediaType: " & objItem.mediaType
          Cells(19, i) = "Name: " & objItem.Name
          Cells(20, i) = "Orientation: " & objItem.Orientation
          Cells(21, i) = "paperLength: " & objItem.paperLength
          Cells(22, i) = "paperSize: " & objItem.PaperSize
          Cells(23, i) = "paperWidth: " & objItem.paperWidth
          Cells(24, i) = "pelsHeight: " & objItem.pelsHeight
          Cells(25, i) = "pelsWidth: " & objItem.pelsWidth
          Cells(26, i) = "printQuality: " & objItem.PrintQuality
          Cells(27, i) = "Scale: " & objItem.Scale
          Cells(28, i) = "SettingID: " & objItem.SettingID
          Cells(29, i) = "specificationVersion: " & objItem.specificationVersion
          Cells(30, i) = "TTOption: " & objItem.TTOption
          Cells(31, i) = "verticalResolution: " & objItem.verticalResolution
          Cells(32, i) = "XResolution: " & objItem.Xresolution
          Cells(33, i) = "YResolution: " & objItem.Yresolution
          Columns(i).AutoFit
      Next
      End Sub
      
      

      1. Contributeur
        Grace au dernier lien, j'ai trouvé le code correspondant. Je n'ai pas pu tester, mon imprimante ne fait pas le recto verso!

        Mettre dans un module:

        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)
        


        Et pour lancer à mettre dans un bouton:

        Option Explicit
        Private Sub CommandButton1_Click()
        SetPrinterToDuplex
        End Sub
        Sub SetPrinterToDuplex()
            SetPrinterDuplex "HP Photosmart 5510 series", 2 'mettre le nom de son imprimante
        End Sub
        'http://www.mrexcel.com/forum/excel-questions/296486-visual-basic-applications-set-duplex-printing.html
        ' ==================================================================
           ' 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
        


        Je t'ai mis le lien

        Bonne programmation
        1. Salut
          J'avais déjà consulté ce document, mais n'ai rien vu correspondant à l'utilisation de Recto/Verso (True ou False).
          Merci quand même.

          Par contre, j'ignore les rôles de :
          BitsPerPel Collate DisplayFlags DitherType ICMIntent
          ICMMethod LogPixels Scale TTOption SpecificationVersion
          Sont-ils important ?