Pour tout ceux qui souhaite imprimer un mshflexgrid. Copier le code suivant dans un module, appeler la fonction à partir de vos forms, sans oublier de copier au prélable un mshflexgrid invisible dans votre formprinicipal ( voir code ). Attention pour que l'impression soit correcte, vous devez impérativement déterminer la propriété .colwidth() de chacune des colonnes de votre mshflexgrid à imprimer.
Source / Exemple :
Public Function PrintGrid(pGrid As MSHFlexGrid, sTitre1 As String, stitre2 As String, pHorizontal As Boolean, msg As Integer) As Long
' pGrid = Mshflexgrid à imprimer
' sTitre1 = Titre principal
'stitre2 = sous titre
' pHorizontal = True pour Landscape
On Error GoTo ErrorImpresion
Dim i As Integer
Dim iMaxRow As Integer
Dim j As Integer
Dim msfGrid As MSHFlexGrid
Dim iPaginas As Integer
Dim hauteur As Long
Dim largeur As Long
Dim mylarge As Long
Dim margegauche As Long
Dim margehaute As Long
Dim coul As String
'Printer.ColorMode = vbPRCMMonochrome
pGrid.Visible = False
Printer.PrintQuality = 160
' fMainForm.MSFlexGrid1 est un mshflexgrid invisible
' msflexgrid
' utilisé uniquement pour cette routine
Set msfGrid = fMainForm.MSHFlexGrid1
msfGrid.FixedCols = 0
msfGrid.Clear
If pHorizontal = True Then
Printer.Orientation = vbPRORLandscape
iMaxRow = 42
Else
Printer.Orientation = vbPRORPortrait
iMaxRow = 55
End If
' calcul du nombre de page
If pGrid.Rows Mod iMaxRow = 0 Then
iPaginas = pGrid.Rows \ iMaxRow
Else
iPaginas = pGrid.Rows \ iMaxRow + 1
End If
msfGrid.Rows = iMaxRow
msfGrid.Cols = pGrid.Cols
If msfGrid.Rows > pGrid.Rows Then 'si moins d'une page on met en proportionnel
msfGrid.Rows = pGrid.Rows
If pHorizontal = True Then
hauteur = (9850 / 42) * pGrid.Rows
Else
hauteur = (13450 / 55) * pGrid.Rows
End If
margehaute = 1750 '+ ((13950 - hauteur) / 2)
Else 'si plus d'une page alors
If pHorizontal = True Then
hauteur = 9850
Else
hauteur = 13450
End If
margehaute = 1750
End If
For i = 0 To pGrid.Cols - 1
msfGrid.ColWidth(i) = pGrid.ColWidth(i)
msfGrid.ColAlignment(i) = 2
largeur = largeur + msfGrid.ColWidth(i)
Next
If pHorizontal = True Then
If largeur < 15000 Then
mylarge = largeur
margegauche = ((15000 - mylarge) / 2) + 200
Else
mylarge = 15000
margegauche = 200
End If
Else
If largeur < 11000 Then
mylarge = largeur
margegauche = ((11000 - mylarge) / 2) + 200
Else
mylarge = 11000
margegauche = 200
End If
End If
'Screen.MousePointer = 11 ' hourglass
'impression d'un logo : modifier le nom de l'image
Printer.PaintPicture menu.Image12.Picture, 250, 250, 700, 700
' imprime titre
Printer.Line (200, 200)-(11000, 200) 'ligne du haut
Printer.Line (200, 1000)-(11000, 1000) ' ligne du bas
Printer.Line (200, 200)-(200, 1000)
Printer.Line (11000, 200)-(11000, 1000)
Printer.CurrentY = 500
Printer.FontName = "Time new roman"
Printer.FontBold = True
Printer.FontSize = 14
X1 = Printer.TextWidth(sTitulo)
If X1 > 11000 Then
Printer.FontSize = 12
X1 = Printer.TextWidth(sTitre1)
If X1 > 11000 Then
Printer.FontSize = 10
X1 = Printer.TextWidth(sTitre1)
If X1 > 11000 Then
Printer.FontSize = 8
End If
End If
End If
Printer.CurrentX = 200 + ((11000 - X1) / 2)
Printer.Print sTitre1
' impression de la date
If pHorizontal = True Then
Printer.CurrentX = 8000
Else
Printer.CurrentX = 8000
End If
Printer.CurrentY = 270
Printer.FontSize = 8
Printer.Print Now & " - Page 1 de " & iPaginas
'impression de titre2
Printer.CurrentX = margegauche
Printer.CurrentY = 1200
Printer.FontSize = 10
Printer.FontBold = True
Printer.Print stitre2
For i = 0 To pGrid.Rows - 2 + iPaginas
If i Mod iMaxRow = 0 And i > 0 Then 'nous sommes en début de nouvelle page
X = i Mod iMaxRow
With msfGrid 'j'imprime la page
.Row = 0
.Col = 0
.ColSel = 0
.RowSel = 0
If pHorizontal Then
Printer.PaintPicture .Picture, margegauche, margehaute, mylarge, hauteur
Else
Printer.PaintPicture .Picture, margegauche, margehaute, mylarge, hauteur
End If
End With
Printer.NewPage
msfGrid.Clear
'il faut recalculer le nombre de lignes qu'il reste à traiter pour calculer la hauteur
'déterminer msfgrid.rows
If pHorizontal = True Then
msfGrid.Rows = pGrid.Rows - ((42 * (i \ iMaxRow)) - 1)
If msfGrid.Rows <= 42 Then 'il n'y aura pas d'autres pages
hauteur = (9850 / 42) * msfGrid.Rows
Else 'il reste pus de 42 lignes donc il y aura une autre page
msfGrid.Rows = 42
hauteur = 9850
End If
Else
msfGrid.Rows = pGrid.Rows - ((55 * (i \ iMaxRow)) - 1)
lplus = i \ iMaxRow + 1
If toto > 2 Then
msfGrid.Rows = msfGrid.Rows + (lplus - 2)
End If
If msfGrid.Rows <= 55 Then 'il n'y aura pas d'autres pages
hauteur = (13450 / 55) * msfGrid.Rows
Else 'il reste pus de 55 lignes donc il y aura une autre page
msfGrid.Rows = 55
hauteur = 13450
End If
End If
For j = 0 To msfGrid.Cols - 1
'reinitialisation des titres
msfGrid.TextMatrix(0, j) = pGrid.TextMatrix(0, j)
Next
'impression du logo
Printer.PaintPicture menu.Image12.Picture, 250, 250, 700, 700
Printer.Line (200, 200)-(11000, 200) 'ligne du haut
Printer.Line (200, 1000)-(11000, 1000) ' ligne du bas
Printer.Line (200, 200)-(200, 1000)
Printer.Line (11000, 200)-(11000, 1000)
Printer.CurrentY = 500
Printer.FontName = "Time new roman"
Printer.FontBold = True
Printer.FontSize = 14
X1 = Printer.TextWidth(sTitre1)
If X1 > 11000 Then
Printer.FontSize = 12
X1 = Printer.TextWidth(sTitre1)
If X1 > 11000 Then
Printer.FontSize = 10
X1 = Printer.TextWidth(sTitre1)
If X1 > 11000 Then
Printer.FontSize = 8
End If
End If
End If
Printer.CurrentX = 200 + ((11000 - X1) / 2)
Printer.Print sTitre1
' Impression de la date et nombre de pages
If pHorizontal = True Then
Printer.CurrentX = 8000
Else
Printer.CurrentX = 8000
End If
Printer.CurrentY = 270
Printer.FontSize = 8
Printer.Print Now & " - Page " & i \ iMaxRow + 1 & " de " & iPaginas
'impression du sous titre
Printer.CurrentX = 200
Printer.CurrentY = 1200
Printer.FontSize = 10
Printer.FontBold = True
Printer.Print stitre2
i = i + 1
End If
For j = 0 To msfGrid.Cols - 1
msfGrid.TextMatrix(i Mod iMaxRow, j) = pGrid.TextMatrix(i - i \ iMaxRow, j)
msfGrid.Row = i Mod iMaxRow
msfGrid.Col = j
pGrid.Row = i - i \ iMaxRow
pGrid.Col = j
'coul = pGrid.CellBackColor
'msfGrid.CellBackColor = coul
Next
Next
With msfGrid
.Row = 0
.Col = 0
.ColSel = 0
.RowSel = 0
If pHorizontal Then
Printer.PaintPicture .Picture, margegauche, margehaute, mylarge, hauteur
Else
Printer.PaintPicture .Picture, margegauche, margehaute, mylarge, hauteur
End If
End With
Printer.EndDoc
pGrid.Visible = True
If msg = 1 Then
MsgBox sTitulo & vbCrLf & iPaginas & " page(s) ont été envoyé à l'imprimante " & Printer.DeviceName, vbInformation, Printer.Port
End If
salir:
Set msfGrid = Nothing
'pubCursorDefault
Exit Function
ErrorImpresion:
Printer.KillDoc
MsgBox "Verifier l'imprimante !!!", vbCritical, "Printer Error"
Resume salir
End Function
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.