|
-
Jun 24th, 2008, 03:39 AM
#1
Thread Starter
Fanatic Member
-
Jun 24th, 2008, 05:54 AM
#2
Re: listview or flexgrid printing
I use the below code in a module for one of my apps to print the contents of ListView. HTH
Code:
Option Explicit
Dim ymax As Single
Dim ymin As Single
Dim xmin As Single
Dim xmax As Single
Dim x As Single
Dim Y As Single
Dim iListCol As Integer
Dim iShow() As Integer
Dim col_wid() As Single
Dim i As Integer
Dim num_cols As Integer
Const MARGIN = 60
Public Sub PrintListView(lvw As ListView, PrintTitle As String)
Const COL_MARGIN = 240
Dim list_item As ListItem
Dim num_subitems As Integer
Dim line_hgt As Single
Dim l As Long
On Error GoTo PrintListView_Error
'Print header title
Printer.CurrentY = 10
Printer.CurrentX = 100 + MARGIN
Printer.FontBold = True
Printer.FontSize = 10
Printer.Print PrintTitle 'print the title
Printer.CurrentY = 10
Printer.CurrentX = Printer.Width - 1750
Printer.Print Format(Date, "dd/mm/yyyy") 'print the date
Printer.FontBold = False
Printer.FontSize = 8.25
xmin = 100
ymin = 300 '100
' ******************
' Get column widths.
num_cols = lvw.ColumnHeaders.Count
ReDim col_wid(1 To num_cols / 2)
ReDim iShow(lvw.ColumnHeaders.Count + 1)
' Set it so that the cols marked SORTED_HIDDEN_COL do not show
For iListCol = 1 To lvw.ColumnHeaders.Count
If lvw.ColumnHeaders(iListCol) = SORTED_HIDDEN_COL Then
iShow(iListCol) = 0
Else
iShow(iListCol) = 1
End If
Next
iListCol = 1
' Check the column headers.
For i = 1 To num_cols Step 2
col_wid(iListCol) = Printer.TextWidth(lvw.ColumnHeaders(i).Text)
'Debug.Print i; "="; col_wid(1)
iListCol = iListCol + 1
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)
'Debug.Print "1="; col_wid(1)
End If
' Check the subitems.
iListCol = 2
For i = 2 To num_subitems Step 2
If col_wid(iListCol) < Printer.TextWidth(list_item.SubItems(i)) Then
col_wid(iListCol) = Printer.TextWidth(list_item.SubItems(i))
'Debug.Print i; "="; col_wid(1)
End If
iListCol = iListCol + 1
Next i
Next list_item
' Add a column margin.
For i = 1 To num_cols / 2
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
iListCol = 1
For i = 1 To num_cols
Printer.CurrentX = x
If iShow(i) = 1 Then
Printer.Print FittedText(lvw.ColumnHeaders(i).Text, col_wid(iListCol));
'Debug.Print FittedText(lvw.ColumnHeaders(i).Text, col_wid(iListCol));
x = x + col_wid(iListCol)
iListCol = iListCol + 1
End If
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 - 2
For Each list_item In lvw.ListItems
'was 47 and 33
If Printer.Orientation = 1 And l = 45 Or _
Printer.Orientation = 2 And l = 31 Then
AddGridLines
AddCopyright
l = 0
Printer.NewPage
Y = ymin + MARGIN + line_hgt / 2
End If
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.
iListCol = 2
For i = 1 To num_subitems
If iShow(i) = 1 Then
Printer.CurrentX = x
Printer.Print FittedText(list_item.SubItems(i + 1), col_wid(iListCol));
'Debug.Print FittedText(list_item.SubItems(i), col_wid(iListCol));
x = x + col_wid(iListCol)
iListCol = iListCol + 1
End If
Next i
Y = Y + line_hgt * 1.5
l = l + 1
Next list_item
AddGridLines
AddCopyright
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
Sub AddGridLines()
ymax = Y
' Draw lines around it all.
Printer.Line (xmin, ymin)-(xmax, ymax), , B
x = xmin - 30 '+ MARGIN / 2
iListCol = 1
For i = 1 To num_cols - 2
If iShow(i) = 1 Then
x = x + col_wid(iListCol)
Printer.Line (x, ymin)-(x, ymax)
iListCol = iListCol + 1
End If
Next i
End Sub
Sub AddCopyright()
'Print header title
Printer.CurrentY = Printer.ScaleHeight - 250
Printer.CurrentX = 100 + MARGIN
Printer.FontBold = True
Printer.FontSize = 10
Printer.Print "Copyright Here"
Printer.FontBold = False
Printer.FontSize = 8.25
End Sub
-
Jun 24th, 2008, 06:40 AM
#3
New Member
Re: listview or flexgrid printing
Hi
You can use to print the msflexgrid using the following method is vb-6
private sub cmdprint_click()
dim i as integer
for i=0 to msflexgrid.rows-1 'to count total rows
if len(msflexgrid1.textmatrix(i,0))>0 then ' to check the row have values
open "lpt1" for output as #1
print #1, " msflexgrid1.textmatrix(i,0) ; msflexgrid1.textmatrix(i,1);msflexgrid1.textmatrix(i,2) 'go upto total columns you want
close #1
end if
next i
end sub
use the above code to print directly to printer
-
Jun 24th, 2008, 06:43 AM
#4
Re: listview or flexgrid printing
Try this for a grid
Code:
Private Sub PrintGrid(MyGrid As MSFlexGrid)
Dim OldWidth As Integer
OldWidth = MyGrid.Width
MyGrid.Width = Printer.Width
Printer.PaintPicture MyGrid.Picture, 0, 0
Printer.EndDoc
MyGrid.Width = OldWidth
End Sub
Private Sub cmdPrintGrid_Click()
Call PrintGrid(MSFlexGrid1)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|