Click to See Complete Forum and Search --> : Printing..
invitro
Feb 24th, 2000, 11:56 AM
Can u print the contents of a listview?
You will need to write some code to do that.
Here is something i ones made and usually use.
Create a class and put the code in there. The MaxY property tells the routine when to raise an even so you can handle thing in there(like printing a footer for your page), and you can set the bRePrintHeader to True so it will print the columnheaders again.
Put this in a class:
Option Explicit
Public Event MaxYReached(ByVal CurrentY As Double, ByRef oPrinter As Object, ByRef bRePrintHeader As Boolean)
Public Sub PrintListview(lvListview As ListView, Optional ByVal oPrinter As Object, Optional ByVal bLines As Boolean = False, Optional ByVal bBlackAndWhite As Boolean = True, Optional ByVal MaxY As Double)
Dim oListItem As ListItem
Dim oSubItem As ListSubItem
Dim oColumnHeader As ColumnHeader
Dim iCounter As Integer
Dim lPrevY As Long
Dim sCorrectPrintText As String
If oPrinter Is Nothing Then
Set oPrinter = Printer
End If
oPrinter.Font = lvListview.Font
Call PrintHeader(lvListview, oPrinter)
oPrinter.CurrentY = oPrinter.CurrentY + oPrinter.TextHeight(" ") + (oPrinter.TextHeight(" ") / 2)
For Each oListItem In lvListview.ListItems
Set oColumnHeader = lvListview.ColumnHeaders(1)
lPrevY = oPrinter.CurrentY
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
oPrinter.CurrentX = oListItem.Left + 60
Case lvwColumnRight
oPrinter.CurrentX = oListItem.Left + oListItem.Width - oPrinter.TextWidth(oColumnHeader.Text) - 120
Case lvwColumnCenter
oPrinter.CurrentX = oListItem.Left + ((oListItem.Width - oPrinter.TextWidth(oListItem.Text)) / 2)
End Select
oPrinter.ForeColor = IIf(bBlackAndWhite, vbBlack, oListItem.ForeColor)
If oListItem.Width > 15 Then
sCorrectPrintText = oListItem.Text
If oPrinter.TextWidth(sCorrectPrintText) > oListItem.Width - 60 Then
Do Until Not oPrinter.TextWidth(sCorrectPrintText & "...") > (oListItem.Width - 120)
sCorrectPrintText = Mid(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
oPrinter.Print sCorrectPrintText;
End If
oPrinter.CurrentY = lPrevY
For Each oSubItem In oListItem.ListSubItems
Set oColumnHeader = lvListview.ColumnHeaders(oSubItem.Index + 1)
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
oPrinter.CurrentX = oColumnHeader.Left + 60
Case lvwColumnRight
oPrinter.CurrentX = oColumnHeader.Left + oColumnHeader.Width - oPrinter.TextWidth(oSubItem.Text) - 120
Case lvwColumnCenter
oPrinter.CurrentX = oColumnHeader.Left + ((oColumnHeader.Width - oPrinter.TextWidth(oSubItem.Text)) / 2)
End Select
oPrinter.ForeColor = IIf(bBlackAndWhite, vbBlack, oSubItem.ForeColor)
If oColumnHeader.Width > 15 Then
sCorrectPrintText = oSubItem.Text
If oPrinter.TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
Do Until Not oPrinter.TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120)
sCorrectPrintText = Mid(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
oPrinter.Print sCorrectPrintText;
End If
oPrinter.CurrentY = lPrevY
Next
'Stop
oPrinter.CurrentY = oPrinter.CurrentY + oListItem.Height
If oPrinter.CurrentY > MaxY Then
Dim bRePrintHeader As Boolean
RaiseEvent MaxYReached(oPrinter.CurrentY, oPrinter, bRePrintHeader)
If bRePrintHeader Then
Call PrintHeader(lvListview, oPrinter)
oPrinter.CurrentY = oPrinter.CurrentY + oPrinter.TextHeight(" ") + (oPrinter.TextHeight(" ") / 2)
End If
End If
Next
End Sub
Public Sub PrintHeader(lvListview As ListView, oPrinter As Object)
Dim lPrevY As Long
Dim oColumnHeader As ColumnHeader
Dim sCorrectPrintText As String
For Each oColumnHeader In lvListview.ColumnHeaders
If oColumnHeader.Width > 60 Then
lPrevY = oPrinter.CurrentY
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
oPrinter.CurrentX = oColumnHeader.Left + 60
Case lvwColumnRight
oPrinter.CurrentX = oColumnHeader.Left + oColumnHeader.Width - oPrinter.TextWidth(oColumnHeader.Text) - 120
Case lvwColumnCenter
oPrinter.CurrentX = oColumnHeader.Left + ((oColumnHeader.Width - oPrinter.TextWidth(oColumnHeader.Text)) / 2)
End Select
oPrinter.FontUnderline = True
If oColumnHeader.Width > 15 Then
sCorrectPrintText = oColumnHeader.Text
If oPrinter.TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
Do Until Not oPrinter.TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120)
sCorrectPrintText = Mid(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
oPrinter.Print sCorrectPrintText;
End If
oPrinter.FontUnderline = False
oPrinter.CurrentY = lPrevY
End If
Next
End Sub
The call the routine like this:
Option Explicit
Private WithEvents PrintListView As Class1
Private Sub Command1_Click()
Set PrintListView = New Class1
PrintListView.PrintListView ListView1, , , , Printer.ScaleHeight - 480
End Sub
Private Sub PrintListView_MaxYReached(ByVal CurrentY As Double, ByRef oPrinter As Object, bRePrintHeader As Boolean)
oPrinter.NewPage
bRePrintHeader = True
End Sub
Hope it helps,
[Edited by Azzmodan on 05-09-2000 at 11:29 PM]
invitro
Feb 25th, 2000, 05:53 AM
Hey thanks alot, the code rocks! Works perfectly! Thanks again, this is really helpfull!
invitro
Mar 31st, 2000, 03:00 AM
Sorry i dint mention it any earlier but there is a problem with your code. For some reason it will only print out the listview right after the program is closed. That means, every time a user wants me to print something he will have to close the program .Is there any way to work around this?
Hmm never saw that last message,
Use Printer.EndDoc right after calling the class, this will tell the printer that the document is finished and it wil start printing
Here is an updated version of the code, the previous one i posted had a problem with the first column.
Option Explicit
Public Event MaxYReached(ByVal CurrentY As Double, ByRef oPrinter As Object, ByRef bRePrintHeader As Boolean)
Public Event Progress(ByVal oNextListItem As ListItem, ByRef oPrinter As Object, ByRef bRePrintHeader As Boolean, ByVal lTotalLines As Long, ByRef bCancel As Boolean)
Private mlFromX As Long
Public Sub PrintListview(lvListview As ListView, Optional ByVal oPrinter As Object, Optional ByVal bLines As Boolean = False, Optional ByVal bBlackAndWhite As Boolean = True, Optional ByVal MaxY As Double, Optional ByVal lFromX As Long)
Dim oListItem As ListItem
Dim oSubItem As ListSubItem
Dim oColumnHeader As ColumnHeader
Dim iCounter As Integer
Dim lPrevY As Long
Dim sCorrectPrintText As String
Dim bRePrintHeader As Boolean
Dim bCancel As Boolean
mlFromX = lFromX
If oPrinter Is Nothing Then
Set oPrinter = Printer
End If
With oPrinter
.Font = lvListview.Font
Call PrintHeader(lvListview, oPrinter)
.CurrentY = .CurrentY + .TextHeight(" ") + (.TextHeight(" ") / 2)
For Each oListItem In lvListview.ListItems
bRePrintHeader = False
RaiseEvent Progress(oListItem, oPrinter, bRePrintHeader, lvListview.ListItems.Count, bCancel)
If bCancel Then
Exit For
End If
If bRePrintHeader Then
Call PrintHeader(lvListview, oPrinter)
End If
Set oColumnHeader = lvListview.ColumnHeaders(1)
lPrevY = .CurrentY
'oPrinter.Line (.CurrentX, .CurrentY)-(.CurrentX + oListItem.Width, .CurrentY)
.CurrentY = lPrevY
.CurrentX = .CurrentX - oListItem.Width
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
.CurrentX = mlFromX + oListItem.Left + 60
Case lvwColumnRight
.CurrentX = mlFromX + oListItem.Left + oListItem.Width - .TextWidth(oColumnHeader.Text) - 120
Case lvwColumnCenter
.CurrentX = mlFromX + oListItem.Left + ((oListItem.Width - .TextWidth(oListItem.Text)) / 2)
End Select
.ForeColor = IIf(bBlackAndWhite, vbBlack, oListItem.ForeColor)
If oListItem.Width > 15 Then
sCorrectPrintText = Trim$(oListItem.Text)
If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 60 Then
Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120)
sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
Debug.Assert sCorrectPrintText = Trim$(oListItem.Text)
oPrinter.Print sCorrectPrintText; 'Because of some weird bug there has to be oPrinter before this
' If i dont add the oPrinter i get a syntax error
End If
.CurrentY = lPrevY
For Each oSubItem In oListItem.ListSubItems
Set oColumnHeader = lvListview.ColumnHeaders(oSubItem.Index + 1)
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
.CurrentX = mlFromX + oColumnHeader.Left + 60
Case lvwColumnRight
.CurrentX = mlFromX + oColumnHeader.Left + oColumnHeader.Width - .TextWidth(oSubItem.Text) - 120
Case lvwColumnCenter
.CurrentX = mlFromX + oColumnHeader.Left + ((oColumnHeader.Width - .TextWidth(oSubItem.Text)) / 2)
End Select
.ForeColor = IIf(bBlackAndWhite, vbBlack, oSubItem.ForeColor)
If oColumnHeader.Width > 15 Then
sCorrectPrintText = Trim$(oSubItem.Text)
If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120) Or (sCorrectPrintText = "")
sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
oPrinter.Print sCorrectPrintText;
End If
.CurrentY = lPrevY
Next
.CurrentY = .CurrentY + oListItem.Height
If .CurrentY > MaxY Then
RaiseEvent MaxYReached(.CurrentY, oPrinter, bRePrintHeader)
If bRePrintHeader Then
Call PrintHeader(lvListview, oPrinter)
.CurrentY = .CurrentY + .TextHeight(" ") + (.TextHeight(" ") / 2)
End If
End If
Next
End With
End Sub
Public Sub PrintHeader(lvListview As ListView, oPrinter As Object)
Dim lPrevY As Long
Dim oColumnHeader As ColumnHeader
Dim sCorrectPrintText As String
With oPrinter
For Each oColumnHeader In lvListview.ColumnHeaders
If oColumnHeader.Width > 60 Then
lPrevY = .CurrentY
Select Case oColumnHeader.Alignment
Case lvwColumnLeft
.CurrentX = mlFromX + oColumnHeader.Left + 60
Case lvwColumnRight
.CurrentX = mlFromX + oColumnHeader.Left + oColumnHeader.Width - .TextWidth(oColumnHeader.Text) - 120
Case lvwColumnCenter
.CurrentX = mlFromX + oColumnHeader.Left + ((oColumnHeader.Width - .TextWidth(oColumnHeader.Text)) / 2)
End Select
.FontUnderline = True
If oColumnHeader.Width > 15 Then
sCorrectPrintText = Trim$(oColumnHeader.Text)
If sCorrectPrintText <> "" Then
If .TextWidth(sCorrectPrintText) > oColumnHeader.Width - 120 Then
Do Until Not .TextWidth(sCorrectPrintText & "...") > (oColumnHeader.Width - 120) Or (sCorrectPrintText = "")
sCorrectPrintText = Mid$(sCorrectPrintText, 1, Len(sCorrectPrintText) - 1)
Loop
sCorrectPrintText = sCorrectPrintText & "..."
End If
oPrinter.Print sCorrectPrintText;
End If
End If
.FontUnderline = False
.CurrentY = lPrevY
End If
Next
End With
End Sub
Hope it helps
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.