John F.
March 27th, 2001, 02:32 AM
Hello, I am trying to learn visual basic 5. My boss has asked me to write a text editor which I have done, but I am having problems with printing.
I have used a text box for my text area and used the printer object to print but the text at the right of the screen will not print.
the code I have used to print is:
texteditor.dialogbox.ShowPrinter
Printer.Print texteditor.textwindow.Text
Printer.EndDoc
any help would be much appreciated.
John F.
Cakkie
March 27th, 2001, 03:54 AM
With the printer object you will need to be sure that all things fit on a line, if not, you will have to cut down the line yourself or the end will be lost. This is code that does this
public Sub PrintThis(strText as string)
Dim strToBePrinted as string
Dim PWidth as Long
Dim FFile as Integer
Dim strLine as string
PWidth = Printer.ScaleWidth
' write text to file
' much easier to seperate line from eachother
FFile = FreeFile
Open App.Path & "\prt.tmp" for Output as #FFile
print #FFile, strText
Close #FFile
' open the file just created
FFile = FreeFile
Open App.Path & "\prt.tmp" for input as #FFile
Do Until EOF(FFile)
' get every line, one by one
Line input #FFile, strLine
strLine = strLine & " " & vbCrLf
Do While Printer.TextWidth(strLine) > PWidth
' line to long to fit page
Dim strLineTmp as string
Dim iPos1 as Long
Dim iPos2 as Long
iPos1 = InStr(strLine, " ")
strLineTmp = Left(strLine, iPos1 - 1)
iPos2 = InStr(iPos1 + 1, strLine, " ")
' build the line word by word, until it won't fit anymore
Do While Printer.TextWidth(strLineTmp & mid(strLine, iPos1, iPos2 - iPos1)) <= PWidth
iPos1 = iPos2
iPos2 = InStr(iPos1 + 1, strLine, " ")
If iPos2 <> 0 then
strLineTmp = strLineTmp & mid(strLine, iPos1, iPos2 - iPos1)
else
Exit Do
End If
Loop
strToBePrinted = strToBePrinted & strLineTmp & vbCrLf
strLine = mid(strLine, iPos1 + 1)
Loop
strToBePrinted = strToBePrinted & strLine
Loop
' we got what we need, so send it to the printer
Printer.print strToBePrinted
Printer.EndDoc
End Sub
Tom Cannaerts
slisse@planetinternet.be
The best way to escape a problem, is to solve it.
John F.
March 27th, 2001, 04:38 AM
Thanks very much, you are a life saver.
John F.