 |
|
 |
ivosetyadi Level: Guest

|
Re: FlexGrid Printing problem
I got another email. The code resulted a blank excel sheet, it said. I did some checking, and offer several issues for the matter:
(1)
The MSHFlexGrid to Excel exporting will work,
only if the MSHFlexGrid.DataSource is initialized
with a recordset with values in it.
Usually the source of the recordset would be from database.
For example:
'----------------------------------------
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.Open "dsn=HRD"
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM employee", con, , , 1
Set flex.DataSource = rs
'----------------------------------------
Now, you can assign flex.DataSource to Excel using:
xlWs.Cells(1, 1).CopyFromRecordset flex.DataSource
(2)
If your MSHFlexGrid's DataSource is not initialized with
another rs, then you can substitute the line
xlWs.Cells(1, 1).CopyFromRecordset flex.DataSource
with
For nRow = 1 To nRows
For nCol = 1 To nCols
With flex
xlWs.Cells(nRow, nCol).Value = _
.TextMatrix(nRow - 1, nCol - 1) & " "
End With
Next
Next
Please be advised that this loop will run in
a very long time if your grid's data is huge.
Doing .CopyFromRecordset is aimed for the speed
of the transfer between VB and Excel ...
(3)
... and that gives me an idea that you can also do this:
(a) first, initialize the grid, not by writing the cells,
but by putting values into the .DataSource.
I haven't tried it yet, but if you can figure it out,
then (b) will run speedy.
(b) do the (speedy) .CopyFromRecordset flex.DataSource
|
|
30-12-2003 at 04:35 AM |
|
|  |
|
|
KIMO Level: Trainee
 Registered: 23-06-2005 Posts: 2
|
Re: FlexGrid Printing problem
Hi,
Could you please tell me how to implement this code!
I tried, it is just not working for me. I could not print my Flexgrid data!
Thanks in advance.
Kimo.
quote: steve_w wrote:
Hi
I believe that is because you are only going to be able to print out what is visible on the form.
Try the following code, I used it and it works ok.
Public Function PrintMSFlexgrid(title As String, orientation As Integer, grdName As MSFlexGrid, Ptr As Printer, line_spaces As Integer)
' Local declares
Dim lRowCount As Long
Dim iColCount As Integer
Dim lRowLoop As Long
Dim iColLoop As Integer
Dim iTabPos As Integer
Dim found_legend As Boolean
Dim print_line As Boolean
Dim top As Integer
Dim outstr As String
Printer.orientation = orientation
' print the title if one has been entered.
If title <> "" Then
Ptr.FontName = grdName.Font
Ptr.FontName = "Comic Sans MS"
Ptr.FontSize = 14
Ptr.Print title
Ptr.Print ""
Ptr.Print ""
End If
'loop through the cells and print them
iTabPos = 0
' Start function
lRowCount = grdName.Rows - 1
iColCount = grdName.Cols - 1
For lRowLoop = 0 To lRowCount
grdName.Row = lRowLoop
For iColLoop = 0 To iColCount
If grdName.ColWidth(iColLoop) <> 0 Then
grdName.Col = iColLoop
' Grab the flexgrid font properties.
Ptr.FontName = "Comic Sans MS" 'grdName.CellFontName
Ptr.FontSize = 8 'grdName.CellFontSize
Ptr.FontBold = grdName.CellFontBold
Ptr.FontItalic = grdName.CellFontItalic
Ptr.FontUnderline = grdName.CellFontUnderline
Ptr.ForeColor = grdName.CellForeColor
Ptr.Print Tab(iTabPos + 1); grdName.Text;
iTabPos = iTabPos + (grdName.CellWidth / 60)
' Printer.NewPage
End If
Next iColLoop
Printer.Print ""
For i = 1 To line_spaces
Printer.Print ""
Next i
iTabPos = 0
Next lRowLoop
Ptr.EndDoc
End Function |
[Edited by vbgen on 30-07-2003 at 07:17 PM GMT]
|
|
23-06-2005 at 10:57 PM |
|
|
|
|
 |
 |