问题
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