'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'Fonction pour imprimer ou exporter la capture d'un userform en pleine page
'quelque soit sa taille
'auteur: patricktoulon
'version beta SANS ZOOM!!!!!
#If VBA7 Then
    Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#Else
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If
Sub snapshotForm()
    keybd_event &H12, 0, 0, 0
    keybd_event &H2C, 0, 0, 0
    keybd_event &H2C, 0, &H2, 0
    keybd_event &H12, 0, &H2, 0
    DoEvents
End Sub
Public Function ImprimerUserFormZoomé(uf As Object, Optional nomFichierPDF As String = "", Optional imprimer As Boolean = False) As Boolean
    Dim fichier As String, ws As Worksheet
    Dim shp As Shape, RnG As Range
    Dim Zoom As Long, width_papier As Double, height_papier As Double
    Const margeSecurite As Double = 0.88
    Dim wRatio As Double, hRatio As Double, dw As Double, rh As Double
    
    
    ' Détermine le chemin du PDF si non fourni
    If nomFichierPDF = "" Then
        fichier = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\UserFormCapture.pdf"
    Else
        fichier = nomFichierPDF
    End If
    
    ' Nouvelle feuille temporaire
    Set ws = Sheets.Add
    
    Set RnG = ws.[a1:g20]
    ws.PageSetup.PrintArea = RnG.Address
    
    ' ws.[a1] = "a"
    
    ' Orientation & dimensions papier
    If uf.Width > uf.Height Then
        ws.PageSetup.Orientation = xlLandscape
        width_papier = Application.CentimetersToPoints(29.7)
        height_papier = Application.CentimetersToPoints(21)
    Else
        ws.PageSetup.Orientation = xlPortrait
        width_papier = Application.CentimetersToPoints(21)
        height_papier = Application.CentimetersToPoints(29.7)
    End If
    
    wRatio = RnG.Width / width_papier
    hRatio = RnG.Height / height_papier
    RnG.ColumnWidth = RnG.ColumnWidth / wRatio
    RnG.RowHeight = RnG.RowHeight / hRatio
    
    ' Capture de l'écran active (le UserForm)
    snapshotForm
    
    ' Cache le formulaire
    uf.Hide
    DoEvents
    
    Do While ws.Shapes.Count = 0
        On Error Resume Next
        ws.Paste
        Err.Clear
    Loop
    Set shp = ws.Shapes(ws.Shapes.Count)
    With ws.PageSetup
        '.FitToPagesWide = 1
        '.FitToPagesTall = 1
        .LeftMargin = 0
        .RightMargin = 0
        .TopMargin = 0
        .BottomMargin = 0
        .HeaderMargin = 0
        .FooterMargin = 0
        .CenterHorizontally = True
        .CenterVertically = True
    End With
    
    ' Aligne l’image sur A1
    shp.Top = 0
    shp.Left = 0
    wRatio = width_papier / RnG.Width
    hRatio = height_papier / RnG.Height
    RnG.ColumnWidth = RnG.ColumnWidth / wRatio
    RnG.RowHeight = RnG.RowHeight / hRatio
    'shp.Width = RnG.Width
    DoEvents
    
    If ws.VPageBreaks.Count > 0 Then
        RnG.Columns(RnG.Columns.Count).ColumnWidth = 0.1
        shp.Width = RnG.Width
        shp.Width = ws.VPageBreaks(1).Location.Left
        If shp.Height > RnG.Height Then shp.Height = RnG.Height
        shp.Top = (RnG.Height - shp.Height) / 2
        shp.Left = (RnG.Width - shp.Width) / 2
    End If
    
    
    '  reste a re inclure ici la selection imprimante pdf en mode window ou l'export
    
    ws.PrintPreview
    
    'ws.PrintOut From:=1, To:=1'on imprime que la page 1 il peut y avoir une page2 vide
    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True
    
    
End Function