Impression dans mshflexgrid

cs_farouha Messages postés 44 Date d'inscription lundi 21 février 2005 Statut Membre Dernière intervention 3 juillet 2006 - 26 mai 2005 à 17:30
greg38bj Messages postés 83 Date d'inscription jeudi 11 mars 2004 Statut Membre Dernière intervention 17 novembre 2008 - 27 mai 2005 à 01:03
slt
je veux imprimer le contenu d'un MSHFLEXGRID qui fai appel a une base de données ADO sous access
merci

1 réponse

greg38bj Messages postés 83 Date d'inscription jeudi 11 mars 2004 Statut Membre Dernière intervention 17 novembre 2008
27 mai 2005 à 01:03
Voici un bout de code que j'ai pour une impression paysage dans un de mes log

'dans un module
Public Function PrintGrid(pGrid As MSHFlexGrid, sTitulo As String, stitulo2 As String, pHorizontal As Boolean, msg As Integer) As Long
' pGrid = The grid to print
' sTitulo = Page Title
' pHorizontal = True for 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
Set msfGrid = frmMain.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
mylarge = 15800
margegauche = 400
End If


' imprime titre
Printer.Line (400, 200)-(16200, 200) 'ligne du haut
Printer.Line (400, 1000)-(16200, 1000) ' ligne du bas
Printer.Line (400, 200)-(400, 1000)
Printer.Line (16200, 200)-(16200, 1000)
Printer.CurrentY = 450
Printer.FontName = "Time new roman"
Printer.FontBold = True

Printer.FontSize = 14
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 12
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 10
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 8
End If
End If
End If
Printer.CurrentX = 200 + ((16000 - X1) / 2)
Printer.Print sTitulo


If pHorizontal = True Then
Printer.CurrentX = 15200
Else
Printer.CurrentX = 8000
End If
Printer.CurrentY = 270
Printer.FontSize = 8
Printer.Print "Page 1 sur " & iPaginas

'impression de titre1
Printer.CurrentX = margegauche
Printer.CurrentY = 1200
Printer.FontSize = 10
Printer.FontBold = True
Printer.Print stitulo2


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)
toto = i \ iMaxRow + 1
If toto > 2 Then
msfGrid.Rows = msfGrid.Rows + (toto - 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 Form1.Image1.Picture, 250, 250, 700, 700
Printer.Line (400, 200)-(16200, 200) 'ligne du haut
Printer.Line (400, 1000)-(16200, 1000) ' ligne du bas
Printer.Line (400, 200)-(400, 1000)
Printer.Line (16200, 200)-(16200, 1000)
Printer.CurrentY = 450
Printer.FontName = "Time new roman"
Printer.FontBold = True

Printer.FontSize = 14
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 12
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 10
X1 = Printer.TextWidth(sTitulo)
If X1 > 16000 Then
Printer.FontSize = 8
End If
End If
End If
Printer.CurrentX = 200 + ((16000 - X1) / 2)
Printer.Print sTitulo


If pHorizontal = True Then
Printer.CurrentX = 15200
Else
Printer.CurrentX = 8000
End If
Printer.CurrentY = 270
Printer.FontSize = 8
Printer.Print "Page " & i \ iMaxRow + 1 & " sur " & iPaginas

'impression de titre1
Printer.CurrentX = margegauche
Printer.CurrentY = 1200
Printer.FontSize = 10
Printer.FontBold = True
Printer.Print stitulo2

i = i + 1 ' deja títulos
End If
For j = 0 To msfGrid.Cols - 1
On Error GoTo laisse_tomber
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
laisse_tomber:


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



'puis dans ta forme
Private Sub Form_Load()
MSHex.ColWidth(1) = 2500
MSHex.ColWidth(2) = 1500
MSHex.ColWidth(3) = 2500
MSHex.ColWidth(4) = 1500
MSHex.ColWidth(5) = 5000
MSHex.ColWidth(6) = 2500
MSHex.ColWidth(7) = 3000
MSHex.ColWidth(8) = 2500
End Sub

PS: code pris sur vbfrance
@ +
greg38bj
0
Rejoignez-nous