winzip icon

Better Line Wrap

Email
Submitted on: 1/25/2015 4:14:00 PM
By: Cesare Imperiali (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB 6.0
Views: 460
 
     Simple and fast Linewrap written with a bit of Vb standards. This one does same job as Sam Kopetzky one plus one feature: it can split lines in same length and if no space is found to split them, it will divide the word with a "-" char (lines will result 1 char longer than max you said!) The Sam Kopetzky code is compatible with all Vb versions, while this one is only for Vb6 - as it uses functions like Split and Join - but it may be easier to understand. If you want to take a look to Sam job, go here: [url]http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=35460&lngWId=1[/url]
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Better Line Wrap
'**************************************
'No copyright on this code. You can use it as you 
'like.
'Module bas code
Option Explicit
 Public Function MyJustify(theWholeText As String, theMaxLenOfLine As Integer, Optional blnForceSplit As Boolean = True) As String
Dim astrForcedLines() As String
Dim lngCounter As Long
Dim theTempString As String
'fragment for newlines
astrForcedLines = Split(theWholeText, vbCrLf)
'for each line (element of array)
'decide if line needs to be divided
'in smallest pieces
For lngCounter = LBound(astrForcedLines) To UBound(astrForcedLines)
 If Len(astrForcedLines(lngCounter)) > theMaxLenOfLine Then
 astrForcedLines(lngCounter) = fnctSplitIt(astrForcedLines(lngCounter), theMaxLenOfLine, blnForceSplit)
 End If
Next lngCounter
'rebuild final string
MyJustify = Join(astrForcedLines, vbCrLf)
 End Function
Private Function fnctSplitIt(strInput As String, Maxlen As Integer, blnForceSplit As Boolean) As String
 Dim intFoundPos As Integer
 Dim theSplitChar As String
 Dim theSplitLen As Integer
 
 'strip unnecessary right spaces
 strInput = RTrim(strInput)
 'the blnForceSplit is to determine if you want
 'fixed len lines where division is made with "-" char
 '(when breaking a word) and vbcrlf
 If blnForceSplit Then
Do While Len(strInput) > Maxlen
 If Mid(strInput, Maxlen, 1) = " " Then
 theSplitChar = ""
 Else
 theSplitChar = "-"
 End If
 fnctSplitIt = fnctSplitIt & Left(strInput, Maxlen) & theSplitChar & vbCrLf
 If Len(strInput) - Maxlen > 0 Then
 strInput = Right(strInput, Len(strInput) - Maxlen)
 'trim non significant left spaces
 strInput = LTrim(strInput)
 End If
Loop
'add last piece of string
fnctSplitIt = fnctSplitIt & strInput
 'If the blnForceSplit is false, then you want this code
 'do divide lines where a break is found (a space), and
 'only if a space is not found, you want it to divide lines
 'like before, with a "-" char when breaking a word
 Else
Do While Len(strInput) > Maxlen
 intFoundPos = InStrRev(strInput, " ", Maxlen)
 If intFoundPos > 0 Then
 fnctSplitIt = fnctSplitIt & Left(strInput, intFoundPos - 1) & vbCrLf
 theSplitLen = intFoundPos
 Else
 fnctSplitIt = fnctSplitIt & Left(strInput, Maxlen - 1) & "-" & vbCrLf
 theSplitLen = Maxlen
 End If
 If Len(strInput) - theSplitLen > 0 Then
strInput = Right(strInput, Len(strInput) + 1 - theSplitLen)
'trim non significant left spaces
strInput = LTrim(strInput)
 End If
Loop
'add last piece of string
fnctSplitIt = fnctSplitIt & strInput
 End If
End Function
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.