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 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
    
    Call CopyUserFormImageToClipboard(GetActiveWindow)
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    
    ' Créer un document temporaire
    Set wdDoc = wdApp.Documents.Add
    wdDoc.PageSetup.PaperSize = 7
    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
    Else
    wdDoc.ExportAsFixedFormat OutputFileName:=PdfFile, ExportFormat:=17, OpenAfterExport:=False, _
    OptimizeFor:=0, Range:=0, Item:=0, IncludeDocProps:=True
    wdApp.ActiveWindow.View.Type = 4
    End If
    
    Do While wdApp.ActiveWindow.View.Type = 4
        DoEvents
        Sleep 100 ' Sleep à ajouter dans la déclaration des API
    Loop
    
    'wdDoc.Close False
    
    ' Quitter Word si on l'a lancé depuis VBA
    'wdApp.Quit SaveChanges:=False
    Set wdDoc = Nothing: Set wdApp = Nothing
End Sub