• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Printing from a VB app

I am using the following code to print the details from a screen in my VB app :

Set OutputObject = Printer

    OutputObject.Print "Print Example"
    OutputObject.Print vbLf
    OutputObject.Print txtDescription
    OutputObject.Print "How do i do this"
    OutputObject.EndDoc

When I the field txtDescription has many lines of text (as appears on the screen), when it is printed it is cut off at the end of the line, instead of wrapping automatically to the next line.  Is there an easy way of doing this, rather than writing a routine to split the string up into several strings based on the width of the page?
Thanks.
0
gbr
Asked:
gbr
  • 2
1 Solution
 
spardogCommented:
go to www.vb-world.net and search the download able files for word warp and there is a good script there
0
 
gbrAuthor Commented:
this doesn't really apply to my problem.
0
 
mark2150Commented:
No, there is no simple way to get the printer to wrap the text. If you use a rich text box you can (sort of) do it, but it's fugly and you lose other formatting options. Here is a routine to wrap text for you:

Call PrintLongText(Text2.Text)


Sub PrintLongText(LongText As String)
'
' This procedure prints a text stream to a printer and
' ensures that words are not split between lines and
' that they wrap as needed.
'
    Printer.CurrentX = leftedge
    Do Until LongText = ""
'
' current word is null
'
        word$ = ""
'
'        look for crlf in text
'
        marker1 = InStr(LongText, vbCrLf)
'
' Look for space in line of text
'
        marker = InStr(LongText, " ")
'
' Give preference to CRLF if present
'
        If marker1 < marker Then
            crflag = True
            marker = marker1
        Else
            crflag = False
        End If
'
        If marker > 0 Then
'
' If we've found a space, get left part as current word and strip
' it off the line
'
            word$ = LTrim$(RTrim$(Left$(LongText, marker - 1)))
            LongText = Right$(LongText, Len(LongText) - marker)
            If Left$(LongText, 1) = Chr$(10) Then LongText = Right$(LongText, Len(LongText) - 1)
        Else
'
' Only one word left, take it and close out line of text
'
            word$ = LTrim$(RTrim$(LongText))
            LongText = ""
        End If
'
' Force a newline if we're getting to the end of the current line
'
        If Printer.TextWidth(word$) + Printer.CurrentX > rightedge - Printer.TextWidth(String$(Len(word$), "Z")) Then crflag = True
'
' Print a space and the next word of the message
'
        If crflag = True Then
            Printer.Print word$
            Printer.CurrentX = leftedge
        Else
            Printer.Print word$; " ";
        End If
'
' Repeat until line of text is gone
'
    Loop
    Printer.Print
    Printer.CurrentX = leftedge
End Sub


That's a lot of code for what should have been a simple internal function. Printing in VB seems to be an after-thought rather than a designed in feature. (sigh)

M

0
 
gbrAuthor Commented:
Thank for your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now