'*****************************************************************************************************
' ___ _ _______ __ _ ____ _ _ _______ ___ _ _ _ ___ _ _.
' // \\ /\\ // // \\ // // // // // // \\ // // // // \\ //| //
' //___// //__\ // //__// // // //__// // // // // // // // // // | //
' // // \\ // // \\ // // // \\ // // // // // // // // // | //
'// // // // // // // //___ // \\ // \\__// //__// //___ \\__// // |//
'****************************************************************************************************
'Fonction pour imprimer ou exporter la capture d'un userform en pleine page
'quelque soit sa taille SANS ZOOM!!!!! sans calcul 21/29.7 sans pourcentage
'auteur: patricktoulon
'version V 3.0
'
'le principe de fonctionnement
'on prend une plage suffisamment grande pour contenir un saut de page vertical et ou horizontal
'ces sauts de pages vont se créer automatiquement
'on inscrit une lettre
'on determine la zonne sur cette plage
' on capture et colle la capture
'on dimentionne la capture collée au width ou height sur page 1 ou range selon la presence de saut de page
'on imprime
'voila l'astuce ici c'est de ne plus contraindre les zone et saut de page forcé automatiquement par excel l'orsqu'on dimanetion a 21/29.7
'****************************************************************************************************
#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
Function PrintFormX(uf As Object, Optional NomFichierPDF As String = "", Optional lPrevieW As Boolean = False)
Dim shp As Shape, rng As Range, W As Double, H As Double, Ws As Worksheet
Set Ws = Sheets.Add
snapshotForm
uf.Hide
DoEvents
Set rng = Ws.[a1:M100]: rng = "X": rng.Font.Color = vbWhite
With Ws.PageSetup
.Orientation = IIf(uf.Width > uf.Height, xlLandscape, xlPortrait)
.LeftMargin = 0: .TopMargin = 0
.RightMargin = 0: .BottomMargin = 0
.HeaderMargin = 0: .FooterMargin = 0
End With
DoEvents
Do While Ws.Shapes.Count = 0
On Error Resume Next
Ws.Paste
Err.Clear
Loop
'determine le width et height valable selon les sauts de page
If Ws.VPageBreaks.Count > 0 Then W = Ws.VPageBreaks(1).Location.Left Else W = rng.Width
If Ws.HPageBreaks.Count > 0 Then H = Ws.HPageBreaks(1).Location.Top Else H = rng.Height
'on rechoppe la shape(la capture)
Set shp = Ws.Shapes(Ws.Shapes.Count)
With shp
.LockAspectRatio = True
shp.Width = W 'même largeur que le width de rng ou saut de page vertical(1)
' apres avoir fait la largeur ,si le height est trop grand meme largeur que rng ou saut de page horizontal(1)
If shp.Height > H Then shp.Height = H
'on centre la shape
.Top = (H - .Height) / 2
.Left = (W - .Width) / 2
End With
'on peut vider les cellules(facultatif (le font est en blanc))
Ws.[a1:M100] = ""
If NomFichierPDF <> "" Then
Ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NomFichierPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, from:=1, to:=1, OpenAfterPublish:=False
Else
If lPrevieW Then
Ws.PrintPreview
Else
Ws.PrintOut from:=1, to:=1
End If
'on peut supprimer la feuille temporaire
Application.DisplayAlerts = False
Ws.Delete
Application.DisplayAlerts = True
End If
uf.Show 0
End Function