XL 2013 Impression d'un userform

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

PHV62

XLDnaute Junior
bonjour le forum
je souhaiterai imprimer un userform avec les informations dans les textbox et combobox grace a un bouton

merci d'avance pour votre aide
ph v
 
Ok merci @job75.
Et pas vu ta remarque @nullosse.
Il est vrai qu'avec la méthode à @nullosse, on peut obtenir une utilisation complète de la feuille.
Il faudrait (ce que je n'ai pas fait, seulement en approximations répétées) savoir calculer:
- wdDoc.InlineShapes(1).ScaleHeight et .ScaleWidth
- savoir soit centrer verticalement (wdDoc.PageSetup.WdVerticalAlignment ne fonctionne pas) ou calculer les marges.

1754132461627.png
 
En fait les .ScaleHeight et .ScaleWidth ne fonctionnent pas en réduction.
Suffit d'utiliser .Height et .Width.
Le centrage vertical est fait via les TopMargin et BottomMargin.

La version de @nullosse avec utilisation pleine page.

Edit: L'impression pleine page en grossissement maximum ça peut être utile, mais perso je vais rester sur l'impression Excel en grossissement maximum avec les marges Excel par défaut qui réservent un peu d'espace autour.
Il est bien sûr possible dans le code de @nullosse de réserver des marges en réduisant la taille de la Shape finale des marges utilisées.
 

Pièces jointes

Dernière édition:
ton fichier ne peut être téléchargé @Dudu2
1754137778277.png



et l'exemple de @nullosse impossible d'y arriver chez moi
j'ai pourtant remplacé les constantes par leur numériques
j'ai fait le prorata pour l’échelle verticale et horizontale
rien n'y fait
je précise que j'ai testé avec un userform avec un ratio 1.414xxxx qui est celui du 29.7/21
autrement dit le userform doit remplir toute la feuille en paysage (moins les petite marges éventuellement demandées
 
ok ca match Impec 👍
j'ai ajouté le percentPage de 0 à 100% comme ça on touche pas au marge puisque c'est centré
VB:
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

Sub PrintWithWord()
    ' 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
    
    Const percentPage = 90 / 100 'exemple ici 90%
    
    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 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

    wdApp.ActiveWindow.View.Type = 4
    
    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
 
re
tiens
j'ai bloqué les close et quit il faudra gérer selon la revoilà avec la toale
VB:
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
 
dommage le blackandwhite n'existe pas pour le page setup d'un document word
sinon full option comme la mienne
VB:
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
        Else
            wdDoc.PrintOut
            wdDoc.Close False
            wdApp.Quit SaveChanges:=False
        End If
    End If
    
    Do While wdApp.ActiveWindow.View.Type = 4
        DoEvents
        Sleep 100 ' Sleep à ajouter dans la déclaration des API
    Loop
    
    '
    
    ' Quitter Word si on l'a lancé depuis VBA
    'wdApp.Quit SaveChanges:=False
    Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
1
Affichages
227
  • Question Question
XL 2019 User Form
Réponses
9
Affichages
328
  • Question Question
Microsoft 365 affichage userform
Réponses
4
Affichages
374
Réponses
2
Affichages
322
  • Question Question
Microsoft 365 Lecture vocale USF
Réponses
5
Affichages
181
Retour