Print a MSFlexGrid
By: Wendy Keil-Gonzales Date: 28/08/2001

With this great code provided by Wendy Keil-Gonzales (with the contribution of some code found on the Web) is very easy to print any MSFlexGrid, on multiple pages too. The code uses an hidden FlexGrid (msfTemp) and the code of the tip Print a MSFlexGrid: the simplest way. The latter, however, could only print grids that fitted on one page.

The first thing is to create a FlexGrid on the form where the FlexGrid to print is located and call it msfTemp. Remember to set the Visible property to False, so that it is invisible.

Next, copy the following code in a command button, for example.

Dim msfTemp, msfGrid As MSFlexGrid
Dim c, r, iRowsPerPage, iPages As Integer

Screen.MousePointer = 11

'Invisible grid from wich to print
Set msfTemp = Me.msfTemp

Set msfGrid = GridToPrint

iRowsPerPage = 43  'numbero of rows per page

msfTemp.FixedCols = 0
msfTemp.Cols = msfGrid.Cols
msfTemp.Clear
msfTemp.Rows = iRowsPerPage

If msfGrid.Rows Mod iRowsPerPage = 0 Then
    iPages = msfGrid.Rows \ iRowsPerPage
Else
    iPages = msfGrid.Rows \ iRowsPerPage + 1
End If

For c = 0 To msfGrid.Cols - 1
    msfTemp.ColWidth(c) = msfGrid.ColWidth(c)
Next c

msfTemp.Width = Printer.Width

For r = 0 To msfGrid.Rows - 2 + iPages
    If r Mod iRowsPerPage = 0 And r <> 0 Then
        With msfTemp
            .Row = 0
            .Col = 0
            .ColSel = 0
            .RowSel = 0
            Printer.PaintPicture .Picture, 720, 1250
        End With
        Printer.NewPage
        msfTemp.Clear
        'Headers
        For c = 0 To msfGrid.Cols - 1
            msfTemp.TextMatrix(0, c) = msfGrid.TextMatrix(0, c)
        Next c
        r = r + 1
    End If
    For c = 0 To msfGrid.Cols - 1
        msfTemp.TextMatrix(r Mod iRowsPerPage, c) = _
        msfGrid.TextMatrix(r - r \ iRowsPerPage, c)
    Next c
Next r

With msfTemp
    .Row = 0
    .Col = 0
    .ColSel = 0
    .RowSel = 0
    Printer.PaintPicture .Picture, 720, 1250
End With

Printer.EndDoc

Set msfTemp = Nothing

Set msfGrid = Nothing

Screen.MousePointer = 0

To let the code work you just need to replace GridToPrint with the name of the MSFlexGrid control to print.

Webmaster - Thanks to Wendy Keil-Gonzales for posting this tip.


Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it