0

I am trying to remove a carriage return from a text box with formatted text:

For i = ThisShape.TextFrame2.TextRange.Runs.Count To 1 Step -1
  ThisText = ThisShape.TextFrame2.TextRange.Runs(i).Text
  ThisText = Replace(ThisText, Chr(13), "")
  ThisShape.TextFrame2.TextRange.Runs(i).Text = ThisText
Next i

For some odd reason, the text box remains with a carriage return. Any help would be appreciated.

David

Cymro
  • 1,201
  • 1
  • 11
  • 29
  • I suspect it might be a line feed `Chr(10)` - try replacing your `Chr(13)` with constant `vbCrLf` – dbmitch May 19 '18 at 15:12
  • Can you replace against the whole text range? `With ThisShape.TextFrame2.TextRange .Text = Replace(.Text, vbCr, "") End With` – dbmitch May 19 '18 at 15:29
  • Can you try removing Chr(11) instead? It appears since 2007 PP has been using that as a Line Break for Other Text and Titles – dbmitch May 19 '18 at 15:47
  • Mitch, had read the post about chr(11). Had tried it already. No luck. You can create a text box with 2 lines, 1 word per line. Make each word a different colour. You will see, there is just no way to get rid of that carriage return while keeping the colours. Odd. – Cymro May 19 '18 at 16:08
  • Are you just trying to make the textboxes word-wrap - is that the point? Can you click `Format Shape | Textbox | Wrap text in shape`? – dbmitch May 19 '18 at 16:45
  • 1
    Goal> Line1Multicolour:Line2Grey -> Duplicate shape, separate horizontally, align vertically. Leftshape:Delete Grey line, RightShape: Delete non grey line. Afterwards, 2 shapes, both without wordwrap. I've given up. I think it's a powerpoint bug. Just thought I'd ask all the same. – Cymro May 19 '18 at 17:36
  • Try using TextRange.Find(Chr(13), PositionOfTheLastCharacter -1) to find special control characters. Chr(13) is often kept from being found. https://stackoverflow.com/questions/31895719/remove-line-break-in-powerpoint-vba/66655036#66655036 – konahn Mar 16 '21 at 12:26

1 Answers1

0

The CLEAN function is rarely used, thus often forgotten.

CLEAN removes non printable characters (ASCII 0 through 31) from the given text. You can use it in VBA:

myStr = Application.WorksheetFunction.Clean(myStr)

...or as an Excel worksheet function:

=CLEAN(A1)

Example:

Sub cleanDemo()

    Dim mystr As String

    'populate string with linefeeds and carriage returns.
    'note: vbLf = chr(10): vbCr = chr(13) : vbCrLf = vbCr+vbLf
    mystr = "A" & vbLf & "B" & vbCr & "C" & vbCrLf & "D"

    'display string and it's length
    MsgBox mystr, , "myStr is " & Len(mystr) & " char long."

    'remove ASCII 0 through 31
    mystr = Application.WorksheetFunction.Clean(mystr)

    'display string and it's length
    MsgBox mystr, , "myStr is " & Len(mystr) & " char long."

End Sub

...returns:

img


List all characters within string:

The procedure lists the characters and their codes found in the specified string:

Sub showAllChars(strIn as String)
'Lists all ASCII character codes used in the specified string.
'Adaptable to UNICODE by changing Asc to AscW and Chr to ChrW
Dim strIn As String, c As String, p As Long

For p = 1 To Len(strIn)
    c = Mid(strIn, p, 1)
    Debug.Print "Pos#" & p & ": Chr(" & Asc(c) & ") =[";
    Select Case Asc(c)
        Case 10
            Debug.Print "LF]", ;
        Case 13
            Debug.Print "CR]", ;
        Case Else
            Debug.Print c & "]", ;
    End Select
    If p / 5 = p \ 5 Then Debug.Print
Next p
Debug.Print "Done. (Length=" & Len(strIn) & ")"
End Sub

Example Output:

Pos#1: Chr(65) =[A]         Pos#2: Chr(66) =[B]         Pos#3: Chr(13) =[CR]        
Pos#4: Chr(67) =[C]         Pos#5: Chr(63) =[?]         Pos#6: Chr(68) =[D]         
Pos#7: Chr(17) =[]          Pos#8: Chr(69) =[E]         Pos#9: Chr(70) =[F]         
Pos#10: Chr(63) =[?]        Pos#11: Chr(71) =[G]        Pos#12: Chr(10) =[LF]       
Pos#13: Chr(72) =[H]        Pos#14: Chr(73) =[I]        Done. (Length=14)

More Information:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • 1
    I cannot find a "worksheet" object in PowerPoint. It's not so much about cleaning strings, it's about deleting "runs" and PowerPoint refusing to delete the carriage return. Please see my code at the top. I am in fact removing the carriage return from the string but it is not removed from the "runs". The same thing happens when deleting "lines". The carriage return remains even through the "lines" is deleted:OriginalShape.TextFrame.TextRange.Lines(2).Delete NewShape.TextFrame.TextRange.Lines(1).Delete, leaves an annoying carriage return. Very odd and seems to be a bug. – Cymro May 20 '18 at 21:43