Greetings!


anybody know how to print msflexgrid and the data contain in this flexgrid. I want to know how to print this flexgrid into my printer.


thank in advance.

Dani AI

Generated

Two practical ways to print an MSFlexGrid cleanly:

  • Snapshot the grid and print the bitmap. That is essentially what shared. It will include the gridlines as you see them on screen. Use Printer.PaintPicture to place the image, and remember that Printer.EndDoc actually submits the job to the spooler, so call NewPage between pages and EndDoc once at the end if you want one multi‑page job. (learn.microsoft.com)

  • Draw the grid to the printer for crisp text/lines (helpful for ’s “boxes around text” request and for finer control than ’s loop). Example skeleton:

' Prints cells with borders (vector, not bitmap)
Private Sub PrintFlexGrid(ByVal g As MSFlexGrid)
  Dim lm As Single, tm As Single, x As Single, y As Single
  Dim r As Long, c As Long, cw As Single, rh As Single, s As String

  Printer.ScaleMode = vbTwips
  Printer.Orientation = vbPRORLandscape
  lm = 720: tm = 720  ' 0.5" margins

  y = tm
  For r = 0 To g.Rows - 1
    x = lm: rh = g.RowHeight(r)
    If y + rh > Printer.ScaleHeight - tm Then Printer.NewPage: y = tm
    For c = 0 To g.Cols - 1
      cw = g.ColWidth(c): s = g.TextMatrix(r, c)
      Printer.CurrentX = x + 80: Printer.CurrentY = y + 80: Printer.Print s
      Printer.Line (x, y)-(x + cw, y + rh), vbBlack, B  ' cell border
      x = x + cw
    Next
    y = y + rh
  Next
  Printer.EndDoc
End Sub

Tips that make either approach predictable:

  • Set Printer.ScaleMode = vbTwips so coordinates match VB’s default; use Printer.TextWidth/TextHeight if you want to center text within a cell; switch to landscape with Printer.Orientation = vbPRORLandscape when you have wide grids. (learn.microsoft.com)

If you need recurring headers (as suggested for reporting scenarios), repeat fixed rows after each NewPage or move to a reporting tool.

Recommended Answers

All 11 Replies

I would also like to know if this can be done.

You need to use reports like Datareport or Crustal report for reportin g purpose. That gives you better options like data foramtting etc.

I would also like to know if this can be done.

Regarding printing of msflexgrid and the data inside the cell address of grid it is possible to print. There is a source code for this syntax in this address:

You can print the msflexgrid and its data.

Just type the printing msflexgrid for search and Visual basic for language to search it faster..


Have a nice day!

This is the way how you can take out data from FlexGrid

For i = 0 To Form1.MSFlexGrid1.Col
    For j = 0 To Form1.MSFlexGrid1.Row
      Print Form1.MSFlexGrid1.TextMatrix(i, j)
    Next j
Next i

ok. thanks.

does it print the grid lines also???

No, I would also like to know how to do that.

So far I used for printing lines statment:

Printer.Line ()-()

ok.


I get requests to print boxes around text.

it would take a lot of work to accomplish.

yes.

try to export values into excel sheet.

I may try that sometime.


thanks for the idea.

Let's try this code.. I've been using it and it works excellent!!

Private Sub cmdPrint_Click()
'Print the grid with the top row as a title row
    FlexGridPrint Me.Grd, , , , , 1
End Sub

And also this..

Function FlexGridPrint(Grd As MSFlexGrid, Optional lOrientation As Long = vbPRORPortrait, Optional ByVal lMaxRowsPerPage As Long = -1, Optional lTopBorder As Long = 1000, Optional lLeftBorder As Long = 1000, Optional lRowsToRepeat As Long = 0) As Boolean
    Dim lRowsPrinted As Long, lRowsPerPage As Long
    Dim lThisRow As Long, lNumRows As Long, lImageHeight As Long, lLastImageTop As Long
    Dim lPrinterPageHeight As Long, lPagesPrinted As Long, lHeadingHeight As Long
    
    On Error GoTo ErrFailed
    Grd.TopRow = 1
    lNumRows = Grd.Rows - 1
    lPrinterPageHeight = Printer.Height
    lRowsPerPage = lMaxRowsPerPage
    lRowsPrinted = lRowsToRepeat

    If lRowsToRepeat Then
        'Calculate the height of the heading row
        For lThisRow = 1 To lRowsToRepeat
            lHeadingHeight = lHeadingHeight + Grd.RowHeight(lThisRow)
        Next
    End If

    Do
        'Calculate the number of rows for this page
        lImageHeight = 0
        lRowsPerPage = 0
        'Setup printer
        Printer.Orientation = lOrientation

        For lThisRow = lRowsPrinted To lNumRows
            lImageHeight = lImageHeight + Grd.RowHeight(lThisRow)
            If lRowsPerPage > lMaxRowsPerPage And lMaxRowsPerPage <> -1 Then
                'Image has required number of rows, subtract height of current row
                lImageHeight = lImageHeight - Grd.RowHeight(lThisRow)
                Exit For
            ElseIf lImageHeight + lTopBorder * 2 + lHeadingHeight > lPrinterPageHeight Then           'Allow the same border at the bottom and top
                'Image is larger than page, subtract height of current row
                lImageHeight = lImageHeight - Grd.RowHeight(lThisRow)
                Exit For
            End If
            lRowsPerPage = lRowsPerPage + 1
        Next
        
        'Print this page
        lPagesPrinted = lPagesPrinted + 1
        If lRowsToRepeat Then
            'Print heading rows
            Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder, , lHeadingHeight, , 0, , lHeadingHeight
            'Print data rows
            Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder + lHeadingHeight, , lImageHeight + lHeadingHeight, , lLastImageTop + lHeadingHeight, , lImageHeight + lHeadingHeight
        Else
            'Print data rows
            Printer.PaintPicture Grd.Picture, lLeftBorder, lTopBorder, , lImageHeight, , lLastImageTop, , lImageHeight
        End If
        
        Printer.EndDoc
        
        'Store printer position
        lRowsPrinted = lRowsPrinted + lRowsPerPage
        lLastImageTop = lLastImageTop + lImageHeight + lHeadingHeight
    
    Loop While lRowsPrinted < lNumRows
    
    FlexGridPrint = True
    
    Exit Function
    
ErrFailed:
    'Failed to print grid
    FlexGridPrint = False
    Debug.Print "Error in FlexGridPrint: " & Err.Description

End Function

Ops..forgot to mention.. Grd is the name for that MSFlexGrid..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.