Using VBA in PowerPoint to change font

不想你离开。 提交于 2019-12-11 12:56:04

问题


How do I use VBA to make the font consistent throughout a PowerPoint presentation?

I'm new to VBA, so the code I used could be completely wrong but here it is:

Sub FontChange()

  Dim sld As Slide
  Dim shp As Shape

   For Each sld In ActivePresentation.Slides
       For Each shp In sld.Shapes
       shp.TextFrame.TextRange.Font

         .Size = 12

         .Name = "Bauhaus 93"

         .Bold = False

         .Color.RGB = RGB(255, 127, 255)
    Next shp
   Next sld
End Sub

Thanks in advance for the help.


回答1:


A few mods to Wayne's version in the event that you want to change text that's not in placeholders. And a few tests to make sure that the shape in question a) CAN contain text (shapes like lines cannot) and if so b) that it HAS some text to modify.

Option Explicit

Sub FontChange()

Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
    If shp.HasTextFrame Then  ' Not all shapes do
    If shp.TextFrame.HasText Then  ' the shape may contain no text
        With shp.TextFrame.TextRange.Font
            .Size = 12
            .Name = "Bauhaus 93"
            .Bold = False
            .Color.RGB = RGB(255, 127, 255)
        End With
    End If
    End If
    Next shp
Next sld
End Sub



回答2:


The code below should work. Note that your original didn't test the 'Type' of shape, or as @Tim pointed out, you were missing the 'With ...'

Also, my test of a text shape had a Type of 14, which is a 'placeholder'. You may need to check for additional types if the code doesn't change some fonts you want changed.

Option Explicit

Sub FontChange()

Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
    If shp.Type = msoPlaceholder Then
        With shp.TextFrame.TextRange.Font
            .Size = 12
            .Name = "Bauhaus 93"
            .Bold = False
            .Color.RGB = RGB(255, 127, 255)
        End With
    End If
    Next shp
Next sld

End Sub



来源:https://stackoverflow.com/questions/34444399/using-vba-in-powerpoint-to-change-font

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!