Attribute VB_Name = "Module1"
Public Function PrintGrid(pGrid As MSFlexGrid, sTitulo As String, stitulo2 As String, pHorizontal As Boolean, msg As Integer) As Long

    On Error GoTo ErrorImpresion
    Dim i As Integer
    Dim iMaxRow As Integer
    Dim j As Integer
    Dim msfGrid As MSFlexGrid
    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
      
    pGrid.Visible = False
    Printer.PrintQuality = 160
    
    Set msfGrid = FormCalculMois.Grid1
    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 = 2050
    Else 'si plus d'une page alors
        If pHorizontal = True Then
            hauteur = 9850
        Else
            hauteur = 13450
        End If
        margehaute = 2050
    End If
    
    For i = 0 To pGrid.Cols - 1
        msfGrid.ColWidth(i) = pGrid.ColWidth(i)

        msfGrid.ColAlignment(i) = flexAlignCenterCenter
        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

    ' 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(sTitulo)
        If X1 > 11000 Then
            Printer.FontSize = 10
            X1 = Printer.TextWidth(sTitulo)
            If X1 > 11000 Then
                Printer.FontSize = 8
            End If
        End If
    End If
    
    Printer.CurrentX = 200 + ((11000 - X1) / 2)
    Printer.Print sTitulo

    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 titre1
    Printer.CurrentX = margegauche
    Printer.CurrentY = 1200
    Printer.FontSize = 10
    Printer.FontBold = True
    
    Printer.Print
    Printer.Print
    Printer.CurrentX = margegauche
    Printer.Print stitulo2

    hauteur = 7000
    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
                .FontWidth = 14
                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
            
            For j = 0 To msfGrid.Cols - 1
                'reinitialisation des titres
                msfGrid.TextMatrix(0, j) = pGrid.TextMatrix(0, j)
            Next

            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(sTitulo)
                If X1 > 11000 Then
                    Printer.FontSize = 10
                    X1 = Printer.TextWidth(sTitulo)
                    If X1 > 11000 Then
                        Printer.FontSize = 8
                    End If
                End If
            End If
            Printer.CurrentX = 200 + ((11000 - X1) / 2)
            Printer.Print sTitulo
            ' 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 = 12
            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 stitulo2
            
            
            i = i + 1
        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

        Next
    Next
    

    Printer.CurrentY = 9270
    Printer.Print
    Printer.CurrentX = margegauche
    Printer.Print "Cotisations"
    Printer.FontBold = False
    Printer.CurrentX = margegauche
    Printer.Print "Nombre : " & FormCalculMois.TextNbCot
    Printer.CurrentX = margegauche
    Printer.Print "Somme Totale : " & FormCalculMois.TextTotalCot
        
    Printer.CurrentY = 9270
    Printer.CurrentX = 4000
    Printer.Print
    Printer.FontBold = True
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Abonnements"
    Printer.FontBold = False
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Nombre : " & FormCalculMois.TextNbAbon
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Somme Totale : " & FormCalculMois.TextTotalAbon
    
    Printer.CurrentY = 9270
    Printer.CurrentX = 7700
    Printer.Print
    Printer.FontBold = True
    Printer.CurrentX = 7700 'margegauche
    Printer.Print "Séances"
    Printer.FontBold = False
    Printer.CurrentX = 7700 'margegauche
    Printer.Print "Nombre : " & FormCalculMois.TextNbSeance
    Printer.CurrentX = 7700 'margegauche
    Printer.Print "Somme Totale : " & FormCalculMois.TextTotalSeance
    
    Printer.CurrentY = 10270
    Printer.CurrentX = margegauche  '4000
    Printer.Print
    Printer.FontBold = True
    Printer.CurrentX = margegauche  '4000 'margegauche
    Printer.Print "Carnets UV"
    Printer.FontBold = False
    Printer.CurrentX = margegauche  '4000 'margegauche
    Printer.Print "Nombre : " & FormCalculMois.TextNbUV
    Printer.CurrentX = margegauche  '4000 'margegauche
    Printer.Print "Somme Totale : " & FormCalculMois.TextTotalUV
    
    Printer.CurrentY = 10270
    Printer.CurrentX = 4000
    Printer.Print
    Printer.FontBold = True
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Produits"
    Printer.FontBold = False
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Nombre : " & FormCalculMois.TextNbProd
    Printer.CurrentX = 4000 'margegauche
    Printer.Print "Somme Totale : " & FormCalculMois.TextTotalProd
    
    Printer.Line (200, 12200)-(11000, 12200) 'ligne du haut
    Printer.Line (200, 13000)-(11000, 13000) ' ligne du bas
    Printer.Line (200, 12200)-(200, 13000)
    Printer.Line (11000, 12200)-(11000, 13000)
    
    Printer.CurrentY = 12400
    Printer.FontName = "Time new roman"
    Printer.FontBold = True

    
    Printer.FontSize = 14
    X1 = Printer.TextWidth("Chiffre d'affaires : " & FormCalculMois.TextCA.Text)
    If X1 > 11000 Then
        Printer.FontSize = 12
        X1 = Printer.TextWidth("Chiffre d'affaires : " & FormCalculMois.TextCA.Text)
        If X1 > 11000 Then
            Printer.FontSize = 10
            X1 = Printer.TextWidth("Chiffre d'affaires : " & FormCalculMois.TextCA.Text)
            If X1 > 11000 Then
                Printer.FontSize = 8
            End If
        End If
    End If
    Printer.CurrentX = 200 + ((11000 - X1) / 2)
    Printer.Print "Chiffre d'affaires : " & FormCalculMois.TextCA.Text

    
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, "Impression en cours...." ' Printer.Port
    End If
salir:
    Set msfGrid = Nothing
    'pubCursorDefault
    Exit Function
ErrorImpresion:
    Printer.KillDoc
    MsgBox "Verifier l'imprimante !!!", vbCritical, "Erreur d'impression"
    Resume salir
End Function



    
