Vba code to replace with chr(13) with space for selected text in powerpoint
Results 1 to 7 of 7

Thread: Vba code to replace with chr(13) with space for selected text in powerpoint
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Nov 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Vba code to replace with chr(13) with space for selected text in powerpoint

    Dear all,

    I am currently working on this code and I need to replace chr(13) with space for the selected text and not for the whole slide. The code below works well with all shapes in active presentation .

    The code is as follows :



    Code:
    Sub Removepb()
    Dim otxR As TextRange
    Dim osld As Slide
    Dim oshp As Shape
    For Each osld In ActivePresentation.Slides
    For Each oshp In osld.Shapes
    If Not oshp.Type = msoPlaceholder Then
    If oshp.HasTextFrame Then
    If oshp.TextFrame.HasText Then
    Set otxR = oshp.TextFrame.TextRange
    otxR.Text = Replace(otxR.Text, Chr(13), "")
    End If
    End If
    End If
    Next oshp
    Next osld
    End Sub
    The suggestions would be welcome to customize this subroutine to run on the selected Text inside a shape.

    Regards

  2. #2
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,745
    Post Thanks / Like
    Mentioned
    66 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Select a shape containing text and run the code

    Code:
    Sub ReplaceText()
        Dim Shp As Shape
        On Error Resume Next
        For Each Shp In ActiveWindow.Selection.ShapeRange
            If Shp.HasTextFrame Then
                With Shp.TextFrame.TextRange
                    .Text = Replace(.Text, Chr(10), "") 
                End With
                Exit For
            End If
        Next Shp
        If Err.Number > 0 Then MsgBox "no shape selected"
    End Sub

    Notes
    .Text = Replace(.Text, Chr(10), "")
    - your original code which you said does exactly what you want

    On Error Resume Next

    - prevents the code failing if run when nothing selected
    Last edited by Yongle; Aug 20th, 2019 at 03:09 AM.

  3. #3
    New Member
    Join Date
    Nov 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Hi Yongle,

    The code is not working

    Thanks for the reply.

  4. #4
    New Member
    Join Date
    Nov 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Hi yongle,

    I have used your subroutine with slight modificactions as mentioned below

    Code:
    Sub ReplaceText()
        Dim Shp As Shape
        On Error Resume Next
        For Each Shp In ActiveWindow.Selection.ShapeRange
            If Shp.HasTextFrame Then
                With Shp.TextFrame.TextRange
                    .Text = Replace(.Text, Chr(13), "")
                End With
                Exit For
            End If
        Next Shp
        If Err.Number > 0 Then MsgBox "no shape selected"
    End Sub
    But the issue it is replacing all text in the shape rather than the selected text.

    Please help if you could check that.


    Thanks for your reply

  5. #5
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,745
    Post Thanks / Like
    Mentioned
    66 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Here you go
    - select a single block of text within a shape and run the code

    Code:
    Sub ReplaceSelectedText()
        Dim Shp As Shape, Pos As Long, LenSelTxt As Long, lenShpTxt As Long
        Dim SelTxt As String, shpTxt As String, leftTxt As String, rightTxt As String
        
        On Error Resume Next
        For Each Shp In ActiveWindow.Selection.ShapeRange
        'selected text
            SelTxt = ActiveWindow.Selection.TextRange
            LenSelTxt = Len(SelTxt)
        'shape text
            shpTxt = Shp.TextFrame.TextRange.Text
            lenShpTxt = Len(shpTxt)
        'to left and right of selected text
            Pos = InStr(1, shpTxt, SelTxt, vbTextCompare)
            leftTxt = Left(shpTxt, Pos - 1)
            rightTxt = Right(shpTxt, lenShpTxt - Pos - LenSelTxt + 1)
        'replace text and reassemble
            SelTxt = Replace(SelTxt, Chr(10), " ")
            shpTxt = leftTxt & SelTxt & rightTxt
            Shp.TextFrame.TextRange.Text = shpTxt
            Exit For
        Next Shp
    End Sub

  6. #6
    New Member
    Join Date
    Nov 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Thanks Yongle

    Saved a lot of time indeed!

  7. #7
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,745
    Post Thanks / Like
    Mentioned
    66 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Vba code to replace with chr(13) with space for selected text in powerpoint

    Thanks for the feedback

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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