Consulting

Results 1 to 15 of 15

Thread: why format text is changed ???

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location

    why format text is changed ???

    Dear all

    I'm using this simple macro to replace double spaces but when i run it the text format is changed ash shown on the screen shots:

    Sub ReplaceText()
    Dim oSld As Slide
    Dim oShp As Shape
    Dim oTxtRng As TextRange
    Dim strFind As String, strReplace As String
    Set oSld = ActiveWindow.View.Slide
    strFind = Space$(2)
    strReplace = Space$(1)
    For Each oShp In oSld.Shapes
    If oShp.HasTextFrame Then
    Set oTxtRng = oShp.TextFrame.TextRange
    While InStr(oTxtRng.Text, strFind) > 0
    oTxtRng = Replace(oTxtRng, strFind, strReplace)
    Wend
    End If
    Next oShp
    End Sub


    111.jpg

    222.jpg

  2. #2
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    I noticed that the reason is that there are 2 different text formats on the same box; Any Help ?

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Any news our VBA experts ?

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You are replacing the whole textrange so it will take on the format of the start. You need to work on each run (new format)

    This is top of my head but should at least get you started.

    Sub ReplaceText()Dim oSld As Slide
    Dim oShp As Shape
    Dim oTxtRng As TextRange
    Dim lngRun As Long
    Dim strFind As String, strReplace As String
    Set oSld = ActiveWindow.View.Slide
    strFind = Space$(2)
    strReplace = Space$(1)
    For Each oShp In oSld.Shapes
    If oShp.HasTextFrame Then
    For lngRun = 1 To oShp.TextFrame.TextRange.Runs.Count ' each run
    Set oTxtRng = oShp.TextFrame.TextRange.Runs(lngRun)
    While InStr(oTxtRng.Text, strFind) > 0
    oTxtRng = Replace(oTxtRng, strFind, strReplace)
    Wend
    Next lngRun
    End If
    Next oShp
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    You are KING Mr. John

  6. #6
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Dear John

    How can i merge the below macro with that one you did ?

    Set regX = CreateObject("vbscript.regexp")
    With regX
    .Global = False
    .Pattern = "([0-9])\.([0-9])"
    End With
    For i = 1 To 10
    strInput = oShp.TextFrame.TextRange.Text
    b_found = regX.Test(strInput)
    If b_found = True Then
    strInput = regX.Replace(strInput, "$1,$2")
    oShp.TextFrame.TextRange = strInput
    End If
    Next i

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    That looks a lot like me coding already - I recognise my style.

    This is the last thing for a while I have paid work to do!

    Sub ReplaceText()
    Dim oSld As Slide
    Dim oShp As Shape
    Dim strinput As String
    Dim oTxtRng As TextRange
    Dim strFind As String, strReplace As String
    Dim regX As Object
    Set regX = CreateObject("vbscript.regexp")
    With regX
    .Global = False
    .Pattern = "([0-9])\.([0-9])"
    End With
    Set oSld = ActiveWindow.View.Slide
    strFind = Space$(2)
    strReplace = Space$(1)
    For Each oShp In oSld.Shapes
    If oShp.HasTextFrame Then
    Set oTxtRng = oShp.TextFrame.TextRange
    While InStr(oTxtRng.Text, strFind) > 0
    oTxtRng = Replace(oTxtRng, strFind, strReplace)
    Wend
    End If
    strinput = oShp.TextFrame.TextRange.Text
    b_found = regX.Test(strinput)
    If b_found = True Then
    strinput = regX.Replace(strinput, "$1,$2")
    oShp.TextFrame.TextRange = strinput
    End If
    
    Next oShp
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Quote Originally Posted by John Wilson View Post
    That looks a lot like me coding already - I recognise my style.

    This is the last thing for a while I have paid work to do!

    Sub ReplaceText()
    Dim oSld As Slide
    Dim oShp As Shape
    Dim strinput As String
    Dim oTxtRng As TextRange
    Dim strFind As String, strReplace As String
    Dim regX As Object
    Set regX = CreateObject("vbscript.regexp")
    With regX
    .Global = False
    .Pattern = "([0-9])\.([0-9])"
    End With
    Set oSld = ActiveWindow.View.Slide
    strFind = Space$(2)
    strReplace = Space$(1)
    For Each oShp In oSld.Shapes
    If oShp.HasTextFrame Then
    Set oTxtRng = oShp.TextFrame.TextRange
    While InStr(oTxtRng.Text, strFind) > 0
    oTxtRng = Replace(oTxtRng, strFind, strReplace)
    Wend
    End If
    strinput = oShp.TextFrame.TextRange.Text
    b_found = regX.Test(strinput)
    If b_found = True Then
    strinput = regX.Replace(strinput, "$1,$2")
    oShp.TextFrame.TextRange = strinput
    End If
    
    Next oShp
    End Sub


    Dear John

    You merged the 2 macros but used the whole text range so the format still changed; I'm asking you how can i merge the 2 replaces macros and used the Runs.count approach ??

  9. #9
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Yes it wass an oversight but all you have to do is copy paste the appropriate section from the original code. Doing and learning is Good!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  10. #10
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Forgive me john; what will be the runs count parameter here:

    Sub use_regex() 
        Dim regX As Object 
        Dim osld As Slide 
        Dim oshp As Shape 
        Dim strInput As String 
        Dim b_found As Boolean 
       
         
        Set regX = CreateObject("vbscript.regexp") 
        With regX 
            .Global = True 
            .Pattern = "(\d)\,(\d)" 
        End With 
        For Each osld In ActivePresentation.Slides 
            For Each oshp In osld.Shapes 
                
                    If oshp.HasTextFrame Then 
                        If oshp.TextFrame.HasText Then 
                            strInput = oshp.TextFrame.TextRange.Text 
                            b_found = regX.Test(strInput) 
                            If b_found = True Then 
                                strInput = regX.Replace(strInput, "$1.$2") 
                                oshp.TextFrame.TextRange = strInput 
                            End If 
                        
                    End If 
                End If 
            Next oshp 
        Next osld 
        Set regX = Nothing 
    End Sub
    Last edited by SamT; 01-18-2016 at 06:23 PM.

  11. #11
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Dear John

    really i tried a lot but i'm not VBA expert like you; all what i need is to use the replace to be per each run for replacing the numbers using Regular Expressions.

    Plz help me

  12. #12
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Let's see what you tried to work with runs. (Not what you lifted straight from my website!)
    Have a look at the original code and then alter the regex part to look at each run.

    Sub ReplaceText()    Dim oSld As Slide
        Dim oShp As Shape
        Dim strinput As String
        Dim oTxtRng As TextRange
        Dim strFind As String, strReplace As String
        Dim regX As Object
        Set regX = CreateObject("vbscript.regexp")
        With regX
            .Global = False
            .Pattern = "([0-9])\.([0-9])"
        End With
        Set oSld = ActiveWindow.View.Slide
        strFind = Space$(2)
        strReplace = Space$(1)
        For Each oShp In oSld.Shapes
            If oShp.HasTextFrame Then
                For lngRun = 1 To oShp.TextFrame.TextRange.Runs.Count ' each run
                    Set oTxtRng = oShp.TextFrame.TextRange.Runs(lngRun)
                    While InStr(oTxtRng.Text, strFind) > 0
                        oTxtRng = Replace(oTxtRng, strFind, strReplace)
                    Wend
                Next lngRun
            End If
            ' count runs here
            strinput = oShp.TextFrame.TextRange.Text ' change this
            b_found = regX.Test(strinput)
            If b_found = True Then
                strinput = regX.Replace(strinput, "$1,$2")
                oShp.TextFrame.TextRange = strinput ' change this
            End If
             'next run
        Next oShp
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  13. #13
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    This is supposed to be the right code but still didn't work:

    ' count runs here
    Set oTxtRng = oShp.TextFrame.TextRange
    For lngRun = 1 To oTxtRng.Runs.Count

    strinput = oTxtRng.Runs(lngRun).Text ' change this
    b_found = regX.Test(strinput)
    If b_found = True Then
    strinput = regX.Replace(strinput, "$1,$2")
    oTxtRng = strinput ' change this
    End If
    'next run
    Next lngRun

  14. #14
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You need to also change here:

    oTxtRng = strinput ' change this

    TO

    oTxtRng.Runs(lngRun) = strinput ' change this
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  15. #15
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    In English: You are the MASTER
    In Arabic: إنت معلم


Posting Permissions

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