在PPT中自动获取相应的CustomLayout

时间:2015-01-21 13:33:13

标签: vba powerpoint powerpoint-vba

我仍然是VBA的初学者。我有一个现有的ppt演示文稿。 我想加载一个模板,并根据它包含的内容(图像和/或文本和/或标题)自动匹配幻灯片和相应的布局。

我现在正在手动完成。

Sub test21()
Call LoadDesign

ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(3)
ActivePresentation.Slides(2).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(7)
ActivePresentation.Slides(3).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(3)

End Sub

Sub LoadDesign()
    ActivePresentation.Designs.Load TemplateName:="C:\myTemplateFile.pot", Index:=1
End Sub

我正在使用VS2008和mso2010。 在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我假设您可以使用此代码段开始并根据您的需求添加更多条件:

Sub SelectSlideLayout(ByVal slideNumber As Integer)
    Dim hasTitle  As Boolean
    Dim hasPicture As Boolean
    Dim hasSubtitle As Boolean
    Dim sh As Shape

    hasTitle = False
    hasPicture = False
    hasSubtitle = False

    With ActivePresentation.Slides(slideNumber)

        For Each sh In .Shapes
            If sh.PlaceholderFormat.Type = ppPlaceholderCenterTitle Then
                hasTitle = True
            ElseIf sh.PlaceholderFormat.Type = ppPlaceholderTitle Then
                hasTitle = True
            ElseIf sh.PlaceholderFormat.Type = ppPlaceholderSubtitle Then
                hasSubtitle = True
            ElseIf sh.PlaceholderFormat.Type = ppPlaceholderPicture Then
                hasPicture = True
            ElseIf sh.PlaceholderFormat.Type = ppPlaceholderBitmap Then
                hasPicture = True
            ElseIf sh.PlaceholderFormat.Type = ppPlaceholderObject Then
                hasPicture = True
            Else
                '' TODO: Specify more cases
            End If
        Next sh

    End With

    If hasTitle And hasSubtitle Then
        ActivePresentation.Slides(slideNumber).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1)
    ElseIf hasTitle And hasPicture Then
        ActivePresentation.Slides(slideNumber).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(2)
    Else
        '' TODO: Specify more cases

        'Default layout
        ActivePresentation.Slides(slideNumber).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1)
    End If
End Sub