We help IT Professionals succeed at work.

Cutting a string onto multiple lines.

NO_CARRIER
NO_CARRIER asked
on
Medium Priority
709 Views
Last Modified: 2013-12-25
I'm trying to cut a string and have it on multiple lines.
I want to do this in a regular multiline textbox by performing a carriage return on every 50th character.
HOWEVER, I don't want to cut the string in a middle of a word.  So it would instead cut on the word
before.

I have this 90% working... it works for some words, but not all.

   Do While strText <> ""
     strSnip = Left(strText, 50)
     If Len(strText) > 50 Then
        If Mid(strText, 51, 1) <> " " Then
           'intJ = InStrRev(strText, " ", 51)
           If intJ > 0 Then
              strSnip = Left(strText, intJ - 1)
              strText = Mid(strText, intJ + 1)
           Else
              strText = Mid(strText, 51)
           End If
        Else
           strText = Mid(strText, 51)
        End If
     Else
        strText = ""
     End If
     strDone = strDone & strSnip & vbCrLf
  Loop
  Format50 = strDone
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi NO_CARRIER,
To save my time, can you please post a sentence where it does not work?

Dabas
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
hmm... that's what I thought too.  I remember I had it working last year... but someone fuddled around in my code and not it's messed up.

on compile it gives an error on the commented line... "Expected array"...

Author

Commented:
I declare the array with
Dim InStrRev() as String

... but on compile it gives a Time mismatch on this line:
  intJ = InStrRev(strtext, " ", 51) '************ Un comment this line


doubled points to 140.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm using VB6 ...

Author

Commented:
If I do not declare I get the error, "Expected Array" highlighting InStrRev...
CERTIFIED EXPERT

Commented:
This is from VB6 help

InStrRev Function
     

Description

Returns the position of an occurrence of one string within another, from the end of string.

Syntax

InstrRev(stringcheck, stringmatch[, start[, compare]])

The InstrRev function syntax has these named arguments:

Part Description
stringcheck Required. String expression being searched.
stringmatch Required. String expression being searched for.
start Optional. Numeric expression that sets the starting position for each search. If omitted, –1 is used, which means that the search begins at the last character position. If start contains
Null, an error occurs.
 
compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See Settings section for values
CERTIFIED EXPERT

Commented:
Maybe your VBA is not up to date.

Press F2 to go into your object browser
Search for InStrRev

If it does not appear then your VBA is out of date.

This function was supplied with VB6 and did not exist in VB5

Dabas
CERTIFIED EXPERT

Commented:
My msvbvm60.dll is dated 28 Aug 2002 and is version 6.0.92.37

Author

Commented:
Hmm... I did F2 (really cool feature by the way, never seen this screen before)... and it does have InStrRev in it.  I'm using VB6 Retail... (not VBA)...

but it should still work, no?

Ah-ha!... I found it.  You got me thinking... I did a project-wide search for InStrRev .. and it's declared globally in a module.  I commented that line and it works again!

Thanks Dabas!
CERTIFIED EXPERT

Commented:
<BIG SMILE>
Glad to be of help.
I would not be able to live without F2.
Indirectly you ARE using VBA. If you go Project->References, you will see that one of the references is to "Visual Basic for Applications"
Also in the F2 screen, you will see that when this function is found, it is shown as belonging to the VBA library.

Dabas

Author

Commented:
Actually, I did notice that it was part of the VBA library.  That's interesting... I always thought VBA was just a stripped down version of VB.. but independant of.  This is definately more efficient.. the more I work with VB the more I like it.  (unfortunately I'm still developing a taste for Java...) :/ ... in either case.  Thanks again for helping.  It's tough debugging when it's not your code --- or in my case, when someone else has been fiddling around in my code.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.