Printing from a VB app

Posted on 1999-01-28
Last Modified: 2008-03-10
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"

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?
Question by:gbr
  • 2

Expert Comment

ID: 1471054
go to and search the download able files for word warp and there is a good script there

Author Comment

ID: 1471055
this doesn't really apply to my problem.
LVL 12

Accepted Solution

mark2150 earned 30 total points
ID: 1471056
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
            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)
' 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
            Printer.Print word$; " ";
        End If
' Repeat until line of text is gone
    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)



Author Comment

ID: 1471057
Thank for your help.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 67
DO Loop not working 4 72
How to debug this code 7 56
passing parameter in sql procedure 9 56
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now