在 PowerPoint 中使用 VBA 更改字体

Using VBA in PowerPoint to change font

如何使用 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

在此先感谢您的帮助。

下面的代码应该可以工作。请注意,您的原件没有测试 'Type' 形状,或者正如@Tim 指出的那样,您错过了 'With ...'

此外,我对文本形状的测试的类型为 14,即 'placeholder'。如果代码没有更改您想要更改的某些字体,您可能需要检查其他类型。

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

结束子

如果您想更改不在占位符中的文本,可以对 Wayne 的版本进行一些修改。并进行一些测试以确保所讨论的形状 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