如何在使用VBA中的正则表达式替换textframe中的文本时保留PowerPoint中的格式 [英] How to preserve formatting in PowerPoint when replacing text in textframe with Regular Expression in VBA

查看:158
本文介绍了如何在使用VBA中的正则表达式替换textframe中的文本时保留PowerPoint中的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨开发者,

我试图使用正则表达式基于某些模式替换powerpoint中的单词&安培; VB。一切都工作正常,但我失去了格式。任何人都可以帮我保留格式?以下是
我的审核代码

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

       对于k = 1到pobj.ActivePresentation.Slides(j).Shapes.Count

           如果pobj.ActivePresentation.Slides(j).Shapes(k).HasTextFrame则为
               如果pobj.ActivePresentation.Slides(j).Shapes(k).TextFrame.HasText那么

                   对于i = 0到UBound(a)

                               设置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")

                         设置obj = Nothing

                   下一个

                 结束如果

           结束如果

     下一个

 下一个



结束次级

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

推荐答案

文本的格式化特征保存在文本行的隐藏段落标记中。因此,为了保持格式化,您可以操作段落对象的文本,其中包括隐藏的段落标记。

The formatting characteristics for the text are held in a hidden paragraph mark of the text line. So in order to maintain the formatting, you manipulate the text of the paragraph object, which includes the hidden paragraph mark.

shp.TextFrame.TextRange.Paragraphs(p).Text = 'Your replacement paragraph.

希望这有帮助


这篇关于如何在使用VBA中的正则表达式替换textframe中的文本时保留PowerPoint中的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆