如何用vba操作ppt的幻灯片母版?

在PPT中,通过幻灯片母版可以批量设置幻灯片的一些共同元素。

在vba中Master对象表示幻灯片母版,可以通过Presentation对象的SlideMaster属性访问Master对象。

在Master对象中CustomLayouts属性表示幻灯片母版下的幻灯片版式,通过CustomLayouts对象的Add方法可以添加一个自定义的幻灯片版式。

如果要在自定义的版式中添加占位符,可以使用Shapes 对象的AddPlaceholder方法。

有了上述的知识,接下来我们介绍用VBA操作PPT母版的常见需求

一、用VBA在PPT幻灯片母版中新建一个版式

Sub QQ1722187970()
    Dim oPPT As Presentation
    Set oPPT = PowerPoint.ActivePresentation
    Dim oMaster As Master
    Dim oCL As CustomLayout
    Set oMaster = oPPT.SlideMaster
    Dim oSP As Shape
    With oMaster
        Set oCL = .CustomLayouts.Add(1)
        With oCL
            .Name = "自定义版式"
        End With
    End With
End Sub

二、用VBA在PPT幻灯片母版中新建一个版式,并且添加占位符

Sub QQ1722187970()
    Dim oPPT As Presentation
    Set oPPT = PowerPoint.ActivePresentation
    Dim oMaster As Master
    Dim oCL As CustomLayout
    Set oMaster = oPPT.SlideMaster
    Dim oSP As Shape
    With oMaster
        Set oCL = .CustomLayouts.Add(1)
        With oCL
            .Name = "自定义版式"
            '添加占位符
            Set oSP = .Shapes.AddPlaceholder(ppPlaceholderTable, 0, 0, 100, 100)
        End With
    End With
End Sub

三、用VBA在PPT幻灯片母版中删除自定义版式

Sub QQ1722187970()
    Dim oPPT As Presentation
    Set oPPT = PowerPoint.ActivePresentation
    Dim oMaster As Master
    Dim oCL As CustomLayout
    Set oMaster = oPPT.SlideMaster
    Dim oSP As Shape
    With oMaster
        For i = .CustomLayouts.Count To 1 Step -1
            Set oCL = .CustomLayouts(i)
            sName = oCL.Name
            '删除名称为自定义的版式
            If sName Like "*自定义*" Then
                oCL.Delete
            End If
        Next i
    End With
End Sub
       

发表评论