Text Wrapping

Results 1 to 2 of 2

Thread: Text Wrapping

  1. #1
    Patrick Moss Guest

    Default Text Wrapping

    I am trying to develop a function that will take text and a integer Maximumlength and format it. The formatting is as follows: Take the text(basically an array-element using a line break as the delimiter) if it is longer than Maxlength then wrap. Now for the tricky part: If there is a SPACE in the last 5 characters then wrap at that character(SPACE), if not wrap with a dash(-) at the end of the Line(MaxLength). I&#039ve tried for three days to get this to work, and have started from square 1 twice to get a fresh perspective. It can&#039t be as hard as I&#039m making it Please somebody help me out, or even point me in the right direction.<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Text Wrapping

    untested, off the top of my head:<BR><BR>*********************<BR><BR>&#060;%< BR>Function WrapText( ByVal text, maxLength )<BR>&nbsp;&nbsp;&nbsp;&nbsp;outText = ""<BR>&nbsp;&nbsp;&nbsp;&nbsp;Do While Len(text) &#062; maxLength<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&#039 starting at maxLength , look backwards for space<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;spaceAt = InStrRev( text, " ", maxLength )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&#039 how far backwards was it to the space?<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;If (maxLength - spaceAt) &#060;= 5 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039 5 or fewer characters...split the line at the space<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outText = outText & Left( text, spaceAt-1 ) & vbNewLine &#039 or other delimiter<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039 and lop off the start of line<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = Mid( text, spaceAt+1 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039 space further back than 5 characters...hyphenate.<BR>&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o utText = outText & Left( text, maxLength-5 ) & "-" & vbNewLine<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039 and again lop that off from the line<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = Mid( text, maxLength-4 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;Loop<BR>&nbsp;&nbsp; &nbsp;&nbsp;&#039 tack on whatever is left over<BR>&nbsp;&nbsp;&nbsp;&nbsp;If len(text) &#062; 0 Then outText = outText & text & vbNewLine<BR>&nbsp;&nbsp;&nbsp;&nbsp;WrapText = outText<BR>End Function<BR><BR>**************************<BR><BR> It could be written more efficiently, to eliminate the need to keep lopping the head off the input text.<BR><BR>If you will use this *a lot* it might be worth trying to do that.<BR><BR>If it doesn&#039t work, and you can&#039t figure out where I goofed, let me know. BillW@chilisoft.com<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts