在PowerPoint中使用VBA来更改字体

时间:2015-12-23 22:02:25

标签: vba powerpoint

如何使用VBA在整个PowerPoint演示文稿中使字体保持一致?

我是VBA的新手,所以我使用的代码可能完全错误,但现在是:

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

如果您想要更改不在占位符中的文本,可以使用Wayne版本的一些mod。还有一些测试来确保问题的形状a)可以包含文本(线条不能形状),如果是这样b)它有一些文本需要修改。

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

答案 1 :(得分:0)

以下代码应该有效。请注意,您的原始版本并没有测试过' Type'形状,或@Tim指出,你错过了' With ...'

另外,我对文本形状的测试类型为14,这是一个“占位符”。如果代码没有更改您想要更改的某些字体,则可能需要检查其他类型。

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