-->>Hope all is well...
-->>I'm trying to print all records in my list view on my form I went through the tread http://www.daniweb.com/software-development/visual-basic-4-5-6/threads/431714/print-from-mshflexgrid-in-vb-6.0 and tried a couple of eamples it was cool and nice but what comes difficult to me is that my form has List View and not the MSHFlexGrid.
-->>I modified the codes to suite my requrements as declaring the variables ByVal flxData As MSHFlexGrid to ByVal flxData As ListView and so forth...
-->>But the problem is that some functions or procedures do not match to those of the list view so stuck with lots of errors...
-->>My I have I modified version of the List View printing of the Grid version posted by AndreRet..
-->>Help please.
The code involving printing of listview is a bit more involved.
The below example prints the data only in a report format and it sizes itself to display all of the values' text. It does not print icons or other ListView display formats.
When the user selects the Print command, the program calls subroutine PrintListView, passing it the ListView control. PrintListView loops through the column headers calculating their widths.
Then for each list item, the program compares the widths of the item and its subitems to the current column widths and makes the widths big enough to hold the items.
The program loops through the columns printing their headers. It uses the column widths it calculated to position the columns. Next the program loops through the items, printing each item and its subitems, again using the calculated column widths.
Finally the program draws lines around the whole thing and between the columns.
THIS only sample code, change it to fit your needs....
Private Sub mnuFilePrint_Click()
Printer.CurrentX = 1440
Printer.CurrentY = 1440
PrintListView ListView1
Printer.EndDoc
End Sub
Private Sub PrintListView(lvw As ListView)
Const MARGIN = 60
Const COL_MARGIN = 240
Dim ymin As Single
Dim ymax As Single
Dim xmin As Single
Dim xmax As Single
Dim num_cols As Integer
Dim column_header As ColumnHeader
Dim list_item As ListItem
Dim i As Integer
Dim num_subitems As Integer
Dim col_wid() As Single
Dim X As Single
Dim Y As Single
Dim line_hgt As Single
xmin = Printer.CurrentX
ymin = Printer.CurrentY
' ******************
' Get column widths.
num_cols = lvw.ColumnHeaders.Count
ReDim col_wid(1 To num_cols)
' Check the column headers.
For i = 1 To num_cols
col_wid(i) = _
Printer.TextWidth(lvw.ColumnHeaders(i).Text)
Next i
' Check the items.
num_subitems = num_cols - 1
For Each list_item In lvw.ListItems
' Check the item.
If col_wid(1) < Printer.TextWidth(list_item.Text) _
Then _
col_wid(1) = Printer.TextWidth(list_item.Text)
' Check the subitems.
For i = 1 To num_subitems
If col_wid(i + 1) < _
Printer.TextWidth(list_item.SubItems(i)) _
Then _
col_wid(i + 1) = _
Printer.TextWidth(list_item.SubItems(i))
Next i
Next list_item
' Add a column margin.
For i = 1 To num_cols
col_wid(i) = col_wid(i) + COL_MARGIN
Next i
' *************************
' Print the column headers.
Printer.CurrentY = ymin + MARGIN
Printer.CurrentX = xmin + MARGIN
X = xmin + MARGIN
For i = 1 To num_cols
Printer.CurrentX = X
Printer.Print FittedText( _
lvw.ColumnHeaders(i).Text, col_wid(i));
X = X + col_wid(i)
Next i
xmax = X + MARGIN
Printer.Print
line_hgt = Printer.TextHeight("X")
Y = Printer.CurrentY + line_hgt / 2
Printer.Line (xmin, Y)-(xmax, Y)
Y = Y + line_hgt / 2
' Print the rows.
num_subitems = num_cols - 1
For Each list_item In lvw.ListItems
X = xmin + MARGIN
' Print the item.
Printer.CurrentX = X
Printer.CurrentY = Y
Printer.Print FittedText( _
list_item.Text, col_wid(1));
X = X + col_wid(1)
' Print the subitems.
For i = 1 To num_subitems
Printer.CurrentX = X
Printer.Print FittedText( _
list_item.SubItems(i), col_wid(i + 1));
X = X + col_wid(i + 1)
Next i
Y = Y + line_hgt * 1.5
Next list_item
ymax = Y
' Draw lines around it all.
Printer.Line (xmin, ymin)-(xmax, ymax), , B
X = xmin + MARGIN / 2
For i = 1 To num_cols - 1
X = X + col_wid(i)
Printer.Line (X, ymin)-(X, ymax)
Next i
End Sub
' Return as much text as will fit in this width.
Private Function FittedText(ByVal txt As String, ByVal wid _
As Single) As String
Do While Printer.TextWidth(txt) > wid
txt = Left$(txt, Len(txt) - 1)
Loop
FittedText = txt
End Function
This sample assumes there is room on the page to fit all of the data. If the ListView contains many items, you may need to split it across more than one page.
-->>Thanks AndreRet I need to study you code much betterIf stuck hope you wont mind to help...
-->>Thaks alot let it be my turn.
It's a pleasure Bile. I'll help where I can. :)
Please mark this as solved, thanx.
-->>Thanks a lot Andre I tested the codes and seems to work just as I wanted though I didn’t send the document to a printer it is still on wait as I'm disconnected to printer.
-->>But I got an alternative of saving it in .PDF format and hence previewed it and everything was as I needed, and thanks for well organized comments it helped me to speed up in going through...
-->>And please the Splitting will be useful to me as well because I do have more Items, and as I pass through I thought may be the object you created in the Gridprinting post as ByVal Ptr As Object and set its orientation Ptr.Orientation = 2 may help am I right?
That is correct, set your orientation to portrait or landscape as you need.
Please post the pdf code you have, on this thread as well for future users so they can see the alternative solution too. thanx man. :)
-->>Ooh...I'm so sorry I didn't mean the code but I have PDF Creator installed in my computer,so when it detects a printed documents it asks me to save it as a PDF for future use I guess.
-->>So thakns a lot again.
No problems. Happy coding :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.