Quantcast
Channel: General Office Development forum
Viewing all articles
Browse latest Browse all 2257

How to preserve formatting in PowerPoint when replacing text in textframe with Regular Expression in VBA

$
0
0

Hi Developers ,

I am trying to replace  words in powerpoint based on some pattern using regular expression & VB . Everything is working fine but I am loosing the formatting .Can anybody help me to preserve the formatting? Below is my code for review

Sub pptRegEx1(pobj As Object)

Dim obj As Object, i As Integer, j As Integer, k As Integer

Dim a(11) As String

a(0) = "USD"
a(1) = "EUR"
a(2) = "CNY"
a(3) = "GBP"
a(4) = "ZAR"
a(5) = "SEK"
a(6) = "RUB"
a(7) = "THB"
a(8) = "IDR"
a(9) = "AUD"
a(10) = "PHP"
a(11) = "SGD"

For j = 1 To pobj.ActivePresentation.Slides.Count
       For k = 1 To pobj.ActivePresentation.Slides(j).Shapes.Count
            If pobj.ActivePresentation.Slides(j).Shapes(k).HasTextFrame Then
                If pobj.ActivePresentation.Slides(j).Shapes(k).TextFrame.HasText Then
                    For i = 0 To UBound(a)
                                Set obj = CreateObject("vbscript.regexp")

                                obj.Global = True
                                obj.IgnoreCase = True
                                obj.MultiLine = True
                                obj.Pattern = "(" & a(i) & ")(\s)+([0-9]*)"
                           pobj.ActivePresentation.Slides(j).Shapes(k).TextFrame.TextRange.Text = obj.Replace(pobj.ActivePresentation.Slides(j).Shapes(k).TextFrame.TextRange.Text, a(i) &"$3")
                          Set obj = Nothing
                    Next
                 End If
           End If
      Next
 Next

End Sub

                                

Viewing all articles
Browse latest Browse all 2257

Trending Articles