Option Explicit
#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare PtrSafe Function GetActiveWindow Lib "user32.dll" () As LongPtr
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
#End If
'uf =l'object userform
'CropCaption =supprime les bordure ou pas (true/false)
'Percentpage=remplace le zoom  de 0 à 100%
'PdfFile = chemin du fichier pdf
Sub PrintFormWithWord(uf As Object, _
                      Optional CropCaption As Boolean = False, _
                      Optional PercentPage As Long = 100, _
                      Optional NoirEtBlanc As Boolean = False, _
                      Optional lPreview As Boolean = False, _
                      Optional PdfFile As String = "")
    
    ' Ajouter word dans les références pour avoir accès aux constantes word
    Dim wdApp As Object ' Word.Application
    Dim wdDoc As Object ' Word.Document
    Dim PaperWidth As Double
    Dim PaperHeight As Double
    Dim TempDouble As Double
    
    Const PaperA4WidthCm = 21
    Const PaperA4HeightCm = 29.7
    
    PercentPage = PercentPage / 100
    
    Call CopyUserFormImageToClipboard(GetActiveWindow)
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    
    ' Créer un document temporaire
    Set wdDoc = wdApp.Documents.Add
    With wdDoc
        .PageSetup.PaperSize = 7
        '.BlackAndWhite = NoirEtBlanc    ' n'existe pas avec Word dommage
    End With
    PaperWidth = 21
    PaperHeight = 29.7
    wdDoc.PageSetup.Orientation = 1 ' paysage
    
    ' Coller le contenu du presse-papiers (image)
    wdDoc.Content.Paste
    
    With wdDoc.InlineShapes(1)
        wdDoc.PageSetup.Orientation = IIf(.Width > .Height, 1, 0)
        If CropCaption Then
            .PictureFormat.CropTop = uf.Height - uf.InsideHeight
            .PictureFormat.CropBottom = 1
            .PictureFormat.CropLeft = 1
            .PictureFormat.CropRight = 1
        End If
        If wdDoc.PageSetup.Orientation = 1 Then
            PaperWidth = Application.CentimetersToPoints(PaperA4HeightCm)
            PaperHeight = Application.CentimetersToPoints(PaperA4WidthCm)
            .Width = (PaperWidth - 2) * PercentPage
            If .Height > PaperHeight - 2 Then .Height = (PaperHeight - 2) * PercentPage
        Else
            PaperWidth = Application.CentimetersToPoints(PaperA4WidthCm)
            PaperHeight = Application.CentimetersToPoints(PaperA4HeightCm)
            .Height = (PaperHeight - 2) * PercentPage
            If .Width > PaperWidth - 2 Then .Width = (PaperWidth - 2) * PercentPage
        End If
        
        .Range.ParagraphFormat.Alignment = 1 'on centre l'image au milieu
    End With
    
    With wdDoc.PageSetup
        .TopMargin = (PaperHeight - wdDoc.InlineShapes(1).Height) / 2
        .BottomMargin = .TopMargin
        .LeftMargin = 0
        .RightMargin = 0
        '.WdVerticalAlignment = wdAlignVerticalCenter
    End With
    
    If PdfFile <> "" Then
        wdDoc.ExportAsFixedFormat OutputFileName:=PdfFile, ExportFormat:=17    ' wdExportFormatPDF
        wdDoc.Close False
        wdApp.Quit SaveChanges:=False
    Else
        If lPreview Then
            wdApp.Activate
            wdApp.ActiveWindow.View.Type = 4
           If NoirEtBlanc Then wdApp.CommandBars.ExecuteMso "FilePrint": ' Exit Sub
             Do While wdApp.ActiveWindow.View.Type = 4
                DoEvents
                Sleep 100 ' Sleep à ajouter dans la déclaration des API
            Loop
        Else
            wdDoc.PrintOut
            End If
     wdDoc.Close False
            wdApp.Quit SaveChanges:=False
       End If
    
   Set wdDoc = Nothing: Set wdApp = Nothing
End Sub