Solved

wordwrap code

Posted on 1998-09-15
3
250 Views
Last Modified: 2008-03-06
Does anyone have (or willing to find) some code for chopping up a string WordWrap style?  

I want to take the string as it appears in the label on the form and send it line by line to the printer object.  I've got printer.currentx, just need to send the string in and print lines out.

Yes, this is simple, but I'm missing something stupid and have to move on...  Why don't I just print the form?  VB doesn't support printing of forms larger than your screen.  Ah well.
0
Comment
Question by:kmarf
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 50 total points
ID: 1435164
Here's a block of code (way too big for what should be a trivial function) that does what you want.

    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(word$) 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

0
 
LVL 1

Author Comment

by:kmarf
ID: 1435165
Well, it doesn't seem to be printing more than the first line, and I seem to be getting different fonts (or missing labels) each time I print.

Very likely, however, that I've gotten something flubbed up beforehand.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1435166
Watch the LONGTEXT var inside the routine. You should be able to single step it and see it build the line a word at a time.

It expects vbCRLF's as delimiters, a vanilla CR/LF pair will get it confused.
0

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

937 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

5 Experts available now in Live!

Get 1:1 Help Now