humberto Level: VB Lord
 Registered: 13-01-2005 Posts: 246
|
Re: I don't know any thing about printing!!How can i print a simple text??
Here's a routine that will print a text file. It may seem a bit
complicated, but it attempts to offer some pagination
functions...
Public Function gb_text_print(ByVal sFile As String, ByVal
sTitle As String)
Const TWIPS_PER_INCH = 1440
Const PAGINATE = 0
Const OUTPUT = 1
Dim fMarginLeft As Single
Dim fMarginRight As Single
Dim fMarginTop As Single
Dim fMarginBottom As Single
Dim fHeaderHeight As Single
Dim fFooterHeight As Single
Dim fPageLeft As Single
Dim fPageTop As Single
Dim fPageWidth As Single
Dim fPageHeight As Single
Dim fLineHeight As Single
Dim iLinesPerPage As Long
Dim iPageCount As Long
Dim iPage As Long
Dim iPageLine As Long
Dim bPrintCached As Boolean
Dim sTitlePrinted As String
Dim sTitlePage As String
Dim bBold As Boolean
Dim iFile As Long
Dim sLine As String
Dim iChar As Long
Dim iMax As Long
Dim iBreak As Long
Dim eTask As Long
Dim bSuccess As Boolean
On Error GoTo PrintError:
bSuccess = False
'these will eventually be incoming parameters
fMarginLeft = 0.5 * TWIPS_PER_INCH
fMarginRight = 0.5 * TWIPS_PER_INCH
fMarginTop = 0.5 * TWIPS_PER_INCH
fMarginBottom = 0.75 * TWIPS_PER_INCH
fHeaderHeight = 0.33 * TWIPS_PER_INCH
fFooterHeight = 0.33 * TWIPS_PER_INCH
'calculate page dimensions
fPageLeft = fMarginLeft
fPageTop = fMarginTop + fHeaderHeight
fPageWidth = Printer.ScaleWidth - (fMarginLeft + fMarginRight)
fPageHeight = Printer.ScaleHeight - (fMarginTop +
fHeaderHeight + fFooterHeight + fMarginBottom)
fLineHeight = Printer.TextHeight("Bob")
iLinesPerPage = Int(fPageHeight / fLineHeight)
'don't bother if we have no room
If (fPageWidth < TWIPS_PER_INCH) Or (fPageHeight <
TWIPS_PER_INCH) Then
GoTo PrintDone:
End If
iPageCount = 0
sTitlePrinted = "Printed: " & Format$(Now, "mm/dd/yyyy hh:nn")
bBold = Printer.FontBold
For eTask = PAGINATE To OUTPUT
'read the file
iFile = FreeFile
Open sFile For Input As iFile
'progress indices
iPage = 0
iPageLine = 0
bPrintCached = False
Do While Not EOF(iFile)
Line Input #iFile, sLine
Do
'look for a break
iMax = 0
iBreak = 0
If Printer.TextWidth(sLine) <= fPageWidth Then
iMax = Len(sLine)
Else
For iChar = Len(sLine) To 1 Step -1
If iMax = 0 Then
If Printer.TextWidth(Left$(sLine, iChar)) <=
fPageWidth Then
iMax = iChar
End If
End If
If (iMax > 0) And (Mid$(sLine, iChar, 1) = " ") Then
iBreak = iChar
Exit For
End If
Next iChar
End If
If iBreak > 0 Then
'use up to and including the break
If eTask = OUTPUT Then
Printer.CurrentX = fPageLeft
Printer.CurrentY = fPageTop + iPageLine *
fLineHeight
Printer.Print Left$(sLine, iBreak)
bPrintCached = True
End If
iPageLine = iPageLine + 1
sLine = Right$(sLine, Len(sLine) - iBreak)
ElseIf iMax > 0 Then
'use up to and including max character
If eTask = OUTPUT Then
Printer.CurrentX = fPageLeft
Printer.CurrentY = fPageTop + iPageLine *
fLineHeight
Printer.Print Left$(sLine, iMax)
bPrintCached = True
End If
iPageLine = iPageLine + 1
sLine = Right$(sLine, Len(sLine) - iMax)
ElseIf Len(sLine) = 0 Then
'blank line
iPageLine = iPageLine + 1
Else
'abort print? not enough room for any characters to be
output
End If
'header and/or footer?
If (iPageLine = 1) And (eTask = OUTPUT) Then
sTitlePage = Format$(iPage + 1, "0") & " of " &
Format$(iPageCount, "0")
Printer.FontBold = True
If fHeaderHeight >= fLineHeight Then
' Printer.CurrentX = fPageLeft
' Printer.CurrentY = fPageTop - fHeaderHeight
' Printer.Print sTitle
Printer.CurrentX = fPageLeft + 0.5 * (fPageWidth -
Printer.TextWidth(sTitle))
Printer.CurrentY = fPageTop - fHeaderHeight
Printer.Print sTitle
' Printer.CurrentX = fPageLeft + fPageWidth -
Printer.TextWidth(sTitlePrinted)
' Printer.CurrentY = fPageTop - fHeaderHeight
' Printer.Print sTitlePrinted
bPrintCached = True
End If
If fFooterHeight >= fLineHeight Then
Printer.CurrentX = fPageLeft + 0.5 * (fPageWidth -
Printer.TextWidth(sTitlePage))
Printer.CurrentY = fPageTop + fPageHeight +
fFooterHeight - fLineHeight
Printer.Print sTitlePage
bPrintCached = True
End If
Printer.FontBold = bBold
End If
'new page
If iPageLine >= iLinesPerPage Then
If eTask = OUTPUT Then
Printer.NewPage
bPrintCached = False
End If
iPage = iPage + 1
iPageLine = 0
End If
Loop Until Len(sLine) = 0
Loop
'cleanup tasks
Select Case eTask
Case PAGINATE
iPageCount = iPage + Sgn(iPageLine)
Case OUTPUT
Printer.EndDoc
End Select
'close the file
Close iFile
iFile = 0
Next eTask
'if we've fallen through to this point, the print was done
bSuccess = True
PrintDone:
On Error Resume Next
'make sure the file gets closed
Close iFile
gb_text_print = bSuccess
Exit Function
PrintError:
Printer.KillDoc
Resume PrintDone:
End Function
|