Solved Splitting a Long String into Multiple Lines (1 Viewer)

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Hello,

Hope you are doing well. I have a pdf/word form which has description text to be entered into three lines therefore I want to split the description string into multiple lines where each line consist of fixed number of characters say 30chrs. Is there a function available which can split a long string into multiple lines based on entered no of chrs per line?

Best Regards
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:23
Joined
Oct 29, 2018
Messages
21,505
There is a Split() function, but it doesn't split based on number of characters. You may have to create your own function for that.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:23
Joined
Feb 28, 2001
Messages
27,254
You can write a function based on the Mid$() function that can take an input string and break it apart in fixed-length chunks.

 

Gasman

Enthusiastic Amateur
Local time
Today, 12:23
Joined
Sep 21, 2011
Messages
14,378
You might be better splitting it in the first place, then concatenate until you exceed your desired length.
 

moke123

AWF VIP
Local time
Today, 07:23
Joined
Jan 11, 2013
Messages
3,933
Something like this?
strIN is the string to split up
strLen is the number of characters you want in the sections

Code:
Sub LoopString(strIN As String, strLen As Integer)

    Dim strStart As Integer
    Dim MaxL As Integer
    Dim i As Integer
    Dim NbrLoops As Integer

    MaxL = Len(strIN)
    NbrLoops = MaxL \ strLen
  
    If MaxL Mod strLen > 0 Then NbrLoops = NbrLoops + 1  'add a loop if there is a remainder in the division

    strStart = 1

    For i = 1 To NbrLoops
      
        Debug.Print Trim(Mid(strIN, strStart, strLen))
      
        strStart = strStart + strLen
      
    Next

End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 12:23
Joined
Sep 21, 2011
Messages
14,378
Something like this?
strIN is the string to split up
strLen is the number of characters you want in the sections

Code:
Sub LoopString(strIN As String, strLen As Integer)

    Dim strStart As Integer
    Dim MaxL As Integer
    Dim i As Integer
    Dim NbrLoops As Integer

    MaxL = Len(strIN)
    NbrLoops = MaxL \ strLen
 
    If MaxL Mod strLen > 0 Then NbrLoops = NbrLoops + 1  'add a loop if there is a remainder in the division

    strStart = 1

    For i = 1 To NbrLoops
     
        Debug.Print Trim(Mid(strIN, strStart, strLen))
     
        strStart = strStart + strLen
     
    Next

End Sub
My thinking was that could split words? so I was thinking of adding enough words until you meet or will go over the limit?
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:23
Joined
Jan 23, 2006
Messages
15,393
Pac-man,

For clarity, the shortened/split text result does NOT have to keep full words and punctuation?
I have an older routine that does what you ask(shortens to <= a stated length), but it does maintain full words and punctuation. If this is what you want, let me know in forum and I will post SplitString().

Sample:
mytest = "The quick brown fox jumps over the lazy dog. Jumpy zebra, who owns the inn, vows to quit thinking coldy of cottage cheese and pretzels. Bob, Carol, Ted and Alice race forward to get to the front of the crowd to see the King's coronation."

1: Call SplitString(mytest, 20, varShortComment())

Result:
Cmt# len short comment

cmt1 20 The quick brown fox
cmt2 20 jumps over the lazy
cmt3 18 dog. Jumpy zebra,
cmt4 18 who owns the inn,
cmt5 13 vows to quit
cmt6 18 thinking coldy of
cmt7 19 cottage cheese and
cmt8 15 pretzels. Bob,
cmt9 15 Carol, Ted and
cmt10 19 Alice race forward
cmt11 20 to get to the front
cmt12 20 of the crowd to see
cmt13 11 the King's
cmt14 12 coronation.

2: Call SplitString(mytest, 26, varShortComment())

Result:
Cmt# len short comment

cmt1 26 The quick brown fox jumps
cmt2 25 over the lazy dog. Jumpy
cmt3 25 zebra, who owns the inn,
cmt4 22 vows to quit thinking
cmt5 24 coldy of cottage cheese
cmt6 26 and pretzels. Bob, Carol,
cmt7 19 Ted and Alice race
cmt8 22 forward to get to the
cmt9 26 front of the crowd to see
cmt10 23 the King's coronation.
 
Last edited:

moke123

AWF VIP
Local time
Today, 07:23
Joined
Jan 11, 2013
Messages
3,933
My thinking was that could split words? so I was thinking of adding enough words until you meet or will go over the limit?

Pac-man,

For clarity, the shortened/split text result does NOT have to keep full words and punctuation?
I have an older routine that does what you ask(shortens to <= a stated length), but it does maintain full words and punctuation.
Too early in the morning and not enough coffee for that kind of math / logic.
 

Josef P.

Well-known member
Local time
Today, 13:23
Joined
Feb 2, 2023
Messages
835
Here's what I tried:
1. split text into individual words
2. join words up to max. line length.
3. join lines to full string.

Code:
Option Compare Database
Option Explicit

Private Sub RunMe()

   Dim TestString As String
   Dim Lines() As String
   Dim Output As String

   TestString = GenerateTestString(1234)
   Debug.Print "Input:"
   Debug.Print TestString
   Debug.Print String(20, "-")

   Lines = GetLines(TestString, 50)
   Output = Join(Lines, vbNewLine)

   Debug.Print "Output:"
   Debug.Print Output
   Debug.Print String(20, "-")

End Sub

Private Function GenerateTestString(ByVal MinLen As Long) As String

   Const BaseText As String = "a1 bb1 ccc1 xxxx1 yyyyy1 zzzzzz1."

   Dim TestString As String
   Dim i As Long

   TestString = BaseText
   i = 1
   Do Until Len(TestString) > MinLen
      i = i + 1
      TestString = TestString & " " & Replace(BaseText, "1", i)
   Loop
   GenerateTestString = TestString

End Function

Private Function GetLines(ByVal LongText2Convert As String, ByVal MaxLen As Long) As String()

   Dim Words() As String
   Dim Lines() As String
   Dim CurrentArrayPos As Long
   Dim WordsMaxIndex As Long
   Dim LineIndex As Long

   Words = Split(LongText2Convert, " ")
   WordsMaxIndex = UBound(Words)
   ReDim Lines(WordsMaxIndex)

   LineIndex = -1
   Do While CurrentArrayPos <= WordsMaxIndex
      LineIndex = LineIndex + 1
      Lines(LineIndex) = GetNextLine(Words, CurrentArrayPos, MaxLen)
   Loop
   ReDim Preserve Lines(LineIndex)
   GetLines = Lines

End Function

Private Function GetNextLine(ByRef Words() As String, ByRef StartIndex As Long, ByVal MaxLen As Long) As String

   Dim i As Long
   Dim Line As String

   Line = Words(StartIndex)
   StartIndex = StartIndex + 1

   For i = StartIndex To UBound(Words)
      If Len(Line & " " & Words(i)) > MaxLen Then
         StartIndex = i
         Exit For
      End If
      Line = Line & " " & Words(i)
   Next

   GetNextLine = Line

End Function
 

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Thank you very much everyone for sparing time to reply.

If this is what you want, let me know in forum and I will post SplitString().
Yes this is exactly what I want. Split the text in multiple lines just like you you demonstrate in the example. It would be so kind of you if you could post it.
For clarity, the shortened/split text result does NOT have to keep full words and punctuation?
Sorry but I didn't understand this question.
Here's what I tried:
1. split text into individual words
2. join words up to max. line length.
3. join lines to full string.

Code:
Option Compare Database
Option Explicit

Private Sub RunMe()

   Dim TestString As String
   Dim Lines() As String
   Dim Output As String

   TestString = GenerateTestString(1234)
   Debug.Print "Input:"
   Debug.Print TestString
   Debug.Print String(20, "-")

   Lines = GetLines(TestString, 50)
   Output = Join(Lines, vbNewLine)

   Debug.Print "Output:"
   Debug.Print Output
   Debug.Print String(20, "-")

End Sub

Private Function GenerateTestString(ByVal MinLen As Long) As String

   Const BaseText As String = "a1 bb1 ccc1 xxxx1 yyyyy1 zzzzzz1."

   Dim TestString As String
   Dim i As Long

   TestString = BaseText
   i = 1
   Do Until Len(TestString) > MinLen
      i = i + 1
      TestString = TestString & " " & Replace(BaseText, "1", i)
   Loop
   GenerateTestString = TestString

End Function

Private Function GetLines(ByVal LongText2Convert As String, ByVal MaxLen As Long) As String()

   Dim Words() As String
   Dim Lines() As String
   Dim CurrentArrayPos As Long
   Dim WordsMaxIndex As Long
   Dim LineIndex As Long

   Words = Split(LongText2Convert, " ")
   WordsMaxIndex = UBound(Words)
   ReDim Lines(WordsMaxIndex)

   LineIndex = -1
   Do While CurrentArrayPos <= WordsMaxIndex
      LineIndex = LineIndex + 1
      Lines(LineIndex) = GetNextLine(Words, CurrentArrayPos, MaxLen)
   Loop
   ReDim Preserve Lines(LineIndex)
   GetLines = Lines

End Function

Private Function GetNextLine(ByRef Words() As String, ByRef StartIndex As Long, ByVal MaxLen As Long) As String

   Dim i As Long
   Dim Line As String

   Line = Words(StartIndex)
   StartIndex = StartIndex + 1

   For i = StartIndex To UBound(Words)
      If Len(Line & " " & Words(i)) > MaxLen Then
         StartIndex = i
         Exit For
      End If
      Line = Line & " " & Words(i)
   Next

   GetNextLine = Line

End Function
Thanks for the code. I'm on phone right now so couldn't check the code. I'll check it.
Something like this?
strIN is the string to split up
strLen is the number of characters you want in the sections

Code:
Sub LoopString(strIN As String, strLen As Integer)

    Dim strStart As Integer
    Dim MaxL As Integer
    Dim i As Integer
    Dim NbrLoops As Integer

    MaxL = Len(strIN)
    NbrLoops = MaxL \ strLen
 
    If MaxL Mod strLen > 0 Then NbrLoops = NbrLoops + 1  'add a loop if there is a remainder in the division

    strStart = 1

    For i = 1 To NbrLoops
     
        Debug.Print Trim(Mid(strIN, strStart, strLen))
     
        strStart = strStart + strLen
     
    Next

End Sub
Thanks for the code, I'll check it too.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:23
Joined
Jan 23, 2006
Messages
15,393
Pac-Man,
As requested. As I said, it's older, but works.

Code:
' ----------------------------------------------------------------
' Procedure Name: SplitString
' Purpose: To restructure long lext into shorter strings of
' less or equal to some specified length keeping full words
' and punctuation.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter strToSplit (String): The long text string to be shortened
' Parameter intMaxLen (Integer): The max length of shortened string
' Parameter varRetArray (Variant()): An array to hold the shortened string/records.
' Author: Jack
' Date: 29-Nov-18
' ----------------------------------------------------------------
Sub SplitString(strToSplit As String, intMaxLen As Integer, varRetArray() As Variant)

10        On Error GoTo SplitString_Error
          '   Parameters and Meaning
          '  strToSplit As String - this is the long text field to whittle down
          '  intMaxLen As Integer - this is the max line length of short comment
          '  varRetArray() As Variant - this is where short comments are made

        '   Local Variables
        Dim varArray As Variant  'will hold individual words
        Dim varTemp() As Variant
        Dim itm As Variant
        Dim strTemp As String    ' the working string
        Dim intTemp As Integer   ' utility integer

        '   Remove all "  " double spaces
20      strTemp = strToSplit    ' the incoming string to split
30      Do While InStr(strTemp, "  ") > 0
40            strTemp = Replace(strTemp, "  ", " ")
50      Loop
        '   Remove all vbCrLf line breaks ================JED
60      strTemp = strToSplit
70      Do While InStr(strTemp, vbCrLf) > 0
80            strTemp = Replace(strTemp, vbCrLf, " ")
90      Loop          '==================================JED

        '   Split string to array
100     varArray = Split(strTemp, " ")

        '   Add " " back to end of each item (for readability on reassembly)
110     For intTemp = 0 To UBound(varArray)
120           varArray(intTemp) = varArray(intTemp) & " "
130     Next intTemp

        '   Create strings of max or shorter length keeping full words
140     strTemp = "": ReDim varTemp(1 To 1)
150     For intTemp = 0 To UBound(varArray)
160           If strTemp <> "" Then
170               strTemp = strTemp & varArray(intTemp)
180           ElseIf strTemp = "" Then
190               strTemp = varArray(intTemp)
200           End If
210           If intTemp = UBound(varArray) Then    'are we finished yet?
220               varTemp(UBound(varTemp)) = strTemp
230               strTemp = ""
240               Exit For
250           End If
260           If Len(strTemp & varArray(intTemp + 1)) > intMaxLen Then
                  ' Do Nothing
270               intTemp = intTemp
280               If IsEmpty(varTemp(UBound(varTemp))) Then
290                   ReDim Preserve varTemp(1 To UBound(varTemp) + 1)
300                   varTemp(UBound(varTemp) - 1) = strTemp
310                   strTemp = ""
320               Else
330               End If

340           Else
350               intTemp = intTemp
360           End If
370     Next intTemp

380     varRetArray = varTemp

390       On Error GoTo 0
SplitString_Exit:
400       Exit Sub

SplitString_Error:

410       MsgBox "Error " & Err.Number & " (" & Err.Description & "), line " & Erl & " in Procedure SplitString" _
         & "  Module  AWFRelated "
420       GoTo SplitString_Exit
End Sub

Sample usage:

Code:
' ----------------------------------------------------------------
' Procedure Name: testLongStr
' Purpose: To break a long string into pieces of length <= X keeping
' full words and or punctuation intact.
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jack
' Date: 29-Aug-18
' ----------------------------------------------------------------
Sub testLongStr()
      Dim varShortComment() As Variant, i As Integer
      Dim mytest As String  'jibberish to make a long string
10    mytest = "The quick brown fox jumps over the lazy dog. Jumpy zebra, who owns the inn, vows to quit thinking coldy of cottage cheese and pretzels. Bob, Carol, Ted and Alice race forward to get to the front of the crowd to see the King's coronation."
20     Call SplitString(mytest, 23, varShortComment())
                  '
                  'create the short comments
30                For i = 1 To UBound(varShortComment)
40                    Debug.Print "cmt" & i & "  " & Len(varShortComment(i)) & "  " & varShortComment(i)
50                Next i
       
End Sub
 

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Pac-Man,
As requested. As I said, it's older, but works.

Code:
' ----------------------------------------------------------------
' Procedure Name: SplitString
' Purpose: To restructure long lext into shorter strings of
' less or equal to some specified length keeping full words
' and punctuation.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter strToSplit (String): The long text string to be shortened
' Parameter intMaxLen (Integer): The max length of shortened string
' Parameter varRetArray (Variant()): An array to hold the shortened string/records.
' Author: Jack
' Date: 29-Nov-18
' ----------------------------------------------------------------
Sub SplitString(strToSplit As String, intMaxLen As Integer, varRetArray() As Variant)

10        On Error GoTo SplitString_Error
          '   Parameters and Meaning
          '  strToSplit As String - this is the long text field to whittle down
          '  intMaxLen As Integer - this is the max line length of short comment
          '  varRetArray() As Variant - this is where short comments are made

        '   Local Variables
        Dim varArray As Variant  'will hold individual words
        Dim varTemp() As Variant
        Dim itm As Variant
        Dim strTemp As String    ' the working string
        Dim intTemp As Integer   ' utility integer

        '   Remove all "  " double spaces
20      strTemp = strToSplit    ' the incoming string to split
30      Do While InStr(strTemp, "  ") > 0
40            strTemp = Replace(strTemp, "  ", " ")
50      Loop
        '   Remove all vbCrLf line breaks ================JED
60      strTemp = strToSplit
70      Do While InStr(strTemp, vbCrLf) > 0
80            strTemp = Replace(strTemp, vbCrLf, " ")
90      Loop          '==================================JED

        '   Split string to array
100     varArray = Split(strTemp, " ")

        '   Add " " back to end of each item (for readability on reassembly)
110     For intTemp = 0 To UBound(varArray)
120           varArray(intTemp) = varArray(intTemp) & " "
130     Next intTemp

        '   Create strings of max or shorter length keeping full words
140     strTemp = "": ReDim varTemp(1 To 1)
150     For intTemp = 0 To UBound(varArray)
160           If strTemp <> "" Then
170               strTemp = strTemp & varArray(intTemp)
180           ElseIf strTemp = "" Then
190               strTemp = varArray(intTemp)
200           End If
210           If intTemp = UBound(varArray) Then    'are we finished yet?
220               varTemp(UBound(varTemp)) = strTemp
230               strTemp = ""
240               Exit For
250           End If
260           If Len(strTemp & varArray(intTemp + 1)) > intMaxLen Then
                  ' Do Nothing
270               intTemp = intTemp
280               If IsEmpty(varTemp(UBound(varTemp))) Then
290                   ReDim Preserve varTemp(1 To UBound(varTemp) + 1)
300                   varTemp(UBound(varTemp) - 1) = strTemp
310                   strTemp = ""
320               Else
330               End If

340           Else
350               intTemp = intTemp
360           End If
370     Next intTemp

380     varRetArray = varTemp

390       On Error GoTo 0
SplitString_Exit:
400       Exit Sub

SplitString_Error:

410       MsgBox "Error " & Err.Number & " (" & Err.Description & "), line " & Erl & " in Procedure SplitString" _
         & "  Module  AWFRelated "
420       GoTo SplitString_Exit
End Sub

Sample usage:

Code:
' ----------------------------------------------------------------
' Procedure Name: testLongStr
' Purpose: To break a long string into pieces of length <= X keeping
' full words and or punctuation intact.
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jack
' Date: 29-Aug-18
' ----------------------------------------------------------------
Sub testLongStr()
      Dim varShortComment() As Variant, i As Integer
      Dim mytest As String  'jibberish to make a long string
10    mytest = "The quick brown fox jumps over the lazy dog. Jumpy zebra, who owns the inn, vows to quit thinking coldy of cottage cheese and pretzels. Bob, Carol, Ted and Alice race forward to get to the front of the crowd to see the King's coronation."
20     Call SplitString(mytest, 23, varShortComment())
                  '
                  'create the short comments
30                For i = 1 To UBound(varShortComment)
40                    Debug.Print "cmt" & i & "  " & Len(varShortComment(i)) & "  " & varShortComment(i)
50                Next i
      
End Sub
Thanks a lot @jdraw . It works as required.
 

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Here's what I tried:
1. split text into individual words
2. join words up to max. line length.
3. join lines to full string.

Code:
Option Compare Database
Option Explicit

Private Sub RunMe()

   Dim TestString As String
   Dim Lines() As String
   Dim Output As String

   TestString = GenerateTestString(1234)
   Debug.Print "Input:"
   Debug.Print TestString
   Debug.Print String(20, "-")

   Lines = GetLines(TestString, 50)
   Output = Join(Lines, vbNewLine)

   Debug.Print "Output:"
   Debug.Print Output
   Debug.Print String(20, "-")

End Sub

Private Function GenerateTestString(ByVal MinLen As Long) As String

   Const BaseText As String = "a1 bb1 ccc1 xxxx1 yyyyy1 zzzzzz1."

   Dim TestString As String
   Dim i As Long

   TestString = BaseText
   i = 1
   Do Until Len(TestString) > MinLen
      i = i + 1
      TestString = TestString & " " & Replace(BaseText, "1", i)
   Loop
   GenerateTestString = TestString

End Function

Private Function GetLines(ByVal LongText2Convert As String, ByVal MaxLen As Long) As String()

   Dim Words() As String
   Dim Lines() As String
   Dim CurrentArrayPos As Long
   Dim WordsMaxIndex As Long
   Dim LineIndex As Long

   Words = Split(LongText2Convert, " ")
   WordsMaxIndex = UBound(Words)
   ReDim Lines(WordsMaxIndex)

   LineIndex = -1
   Do While CurrentArrayPos <= WordsMaxIndex
      LineIndex = LineIndex + 1
      Lines(LineIndex) = GetNextLine(Words, CurrentArrayPos, MaxLen)
   Loop
   ReDim Preserve Lines(LineIndex)
   GetLines = Lines

End Function

Private Function GetNextLine(ByRef Words() As String, ByRef StartIndex As Long, ByVal MaxLen As Long) As String

   Dim i As Long
   Dim Line As String

   Line = Words(StartIndex)
   StartIndex = StartIndex + 1

   For i = StartIndex To UBound(Words)
      If Len(Line & " " & Words(i)) > MaxLen Then
         StartIndex = i
         Exit For
      End If
      Line = Line & " " & Words(i)
   Next

   GetNextLine = Line

End Function
Thanks @Josef P. This code also gives the same results as that of the code provided by @jdraw. Thanks a lot.
 

moke123

AWF VIP
Local time
Today, 07:23
Joined
Jan 11, 2013
Messages
3,933
This was an interesting exercise.

I came up with this which seems to work ok.

Code:
Function LoopString(strIN As String, strLen As Integer) As String
 
    Dim i As Integer, K As Variant, strOut As String
    Dim strA As String, strB As String, strLeftOver As String
   
    Dim col As New Collection
   
    Do While InStr(strIN, "  ") > 0
        strIN = Replace(strIN, "  ", " ")
    Loop

    Do While InStr(strIN, vbCrLf) > 0
        strIN = Replace(strIN, vbCrLf, " ")
    Loop
   
    strLeftOver = strIN
   
    Do Until Len(strLeftOver) = 0

        strA = Mid(strLeftOver, 1, strLen)

        If Len(strLeftOver) < strLen Then
       
            col.Add strLeftOver
            strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strLeftOver))

        Else

            strB = Mid(strA, 1, InStrRev(strA, " "))
            col.Add strB

            strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strB))

        End If

    Loop

    For Each K In col
        Debug.Print K
        strOut = strOut & K & vbNewLine
    Next

    LoopString = strOut
   
End Function
 

Josef P.

Well-known member
Local time
Today, 13:23
Joined
Feb 2, 2023
Messages
835
Tip: I would not change function parameter values upwards.

Code:
Dim InputString As String
Dim OutputString As String

InputString = "ab     cd".
Debug.Print "Input before LoopString", InputString
'=> ab     cd
OutputString = LoopString(InputString, 40)
Debug.Print "Input after LoopString", InputString
' => 'ab cd'
=> ByRef vs ByVal
Code:
Function LoopString(strIN As String, strLen As Integer) As String
vs
Function LoopString(ByVal strIN As String, ByVal strLen As Integer) As String
 

Josef P.

Well-known member
Local time
Today, 13:23
Joined
Feb 2, 2023
Messages
835
For interest:
I have changed the code from #14 as I would write it (without changing the flow or logic).
To me, the design is clearer and easier to maintain with multiple procedures than with a single large procedure. (single-responsibility principle)
How do you handle this?

Code:
Public Function LoopString2(ByVal strIN As String, ByVal MaxLineLen As Long) As String

    Dim strOut As String
    Dim LinesCol As Collection

    strIN = ClearDoubleSpaces(strIN)
    strIN = RemoveLineBreaks(strIN, " ")
    '?: Call first RemoveLineBreakes and then ClearDoubleSpaces?

    Set LinesCol = GetLines(strIN, MaxLineLen)
    strOut = JoinStringCollection(LinesCol, vbNewLine)

    LoopString2 = strOut

End Function

Private Function GetLines(ByVal strIN As String, ByVal MaxLineLen As Long) As Collection

    Dim strLeftOver As String
    Dim LinesCol As Collection

    Set LinesCol = New Collection
    strLeftOver = strIN

    Do Until Len(strLeftOver) = 0
        LinesCol.Add GetNextLine(strLeftOver, MaxLineLen)
    Loop

    Set GetLines = LinesCol

End Function

Private Function GetNextLine(ByRef strLeftOver As String, ByVal MaxLineLen As Long) As String

    Dim strA As String, strB As String

    If Len(strLeftOver) < MaxLineLen Then
        GetNextLine = strLeftOver
        'strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strLeftOver)) '!:  Len(strLeftOver) - Len(strLeftOver) = 0 =>
        strLeftOver = vbNullString
    Else
        strA = Mid(strLeftOver, 1, MaxLineLen)
        strB = Mid(strA, 1, InStrRev(strA, " "))
        'strB = Mid(strLeftOver, 1, InStrRev(strLeftOver, " ", MaxLineLen)) ' should be the same
        '!: Bug: if a word is greather then MaxLineLen => empty line => endless loop
        GetNextLine = strB
        strLeftOver = Right(strLeftOver, Len(strLeftOver) - Len(strB))
    End If

End Function

Private Function JoinStringCollection(ByVal StringCollection As Collection, Optional ByVal Delimiter As String = vbNewLine) As String

    Dim strOut As String
    Dim varItem As Variant

    For Each varItem In StringCollection
        'Debug.Print varItem
        strOut = strOut & varItem & vbNewLine '?: end with vbnewline?
    Next

    JoinStringCollection = strOut

End Function

Private Function ClearDoubleSpaces(ByVal StringToClear As String) As String

    Const Quote As String = " "

    Do While InStr(StringToClear, Quote & Quote) > 0
         StringToClear = Replace(StringToClear, Quote & Quote, Quote)
    Loop
    ClearDoubleSpaces = StringToClear

End Function

Private Function RemoveLineBreaks(ByVal StringToClear As String, Optional ByVal ReplacementString As String = " ") As String
   RemoveLineBreaks = Replace(StringToClear, vbNewLine, ReplacementString)
   '?: do it also with vbCr and/or vbLf?
End Function
 
Last edited:

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Hi,

I was trying to put another possibility by modifying code by @jdraw in #11. Possibility to hyphenate the next word if it is larger than 7 characters and by adding it make the line length longer than the limit but I'm not successful yet. I'll post it if succeeded.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:23
Joined
Jan 23, 2006
Messages
15,393
Why not just increase the line length?
 

Pac-Man

Active member
Local time
Today, 16:23
Joined
Apr 14, 2020
Messages
416
Why not just increase the line length?
Because these is limit of characters in the form I'm inserting data in. Characters above that limit go beyond the visible area.
 

Users who are viewing this thread

Top Bottom