代码之家  ›  专栏  ›  技术社区  ›  Dudi

如何确定正在使用的占位符?

  •  4
  • Dudi  · 技术社区  · 16 年前

    给定一张幻灯片,如何才能确定PowerPoint中是否使用了所有幻灯片布局占位符?

    如果不使用占位符,添加图片时是否可以阻止自动使用占位符?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Todd Main    16 年前

    您需要循环浏览幻灯片上的所有占位符,确定每个占位符的类型,然后检查它是否按预期的格式填充。有18个 PpPlaceholderType ,因此您必须将它们全部设置好,但下面是一个示例,说明您可以如何检查占位符是否在使用中。

    Sub CheckPlaceholders()
    Dim ap As Presentation: Set ap = ActivePresentation
    Dim sl As Slide: Set sl = ap.Slides(2)
    Dim shs As Shapes: Set shs = sl.Shapes
    Dim ph As Placeholders: Set ph = shs.Placeholders
    Dim p As Shape
        For Each p In ph
            Select Case p.Type
            Case PpPlaceholderType.ppPlaceholderHeader
                If p.TextFrame.HasText Then
                    Debug.Print "This Placeholder is in use"
                End If
            Case PpPlaceholderType.ppPlaceholderChart
                If p.HasChart Then
                    Debug.Print "This Placeholder is in use"
                End If
            End Select
        Next
    End Sub
    

    例如,要插入一张图片而不是到达占位符,我找到的唯一方法是创建一个循环来添加图片,直到其中一张图片超出占位符,然后删除已经插入的图片。

    Sub AddPicture()
        Dim pic As String
        pic = "C:\Users\Me\Desktop\beigeplum.jpg"
        Dim ap As Presentation: Set ap = ActivePresentation
        Dim sl As Slide: Set sl = ap.Slides(1)
        Dim sh As Shape
    
        Do
            Set sh = sl.Shapes.AddPicture(pic, msoFalse, msoTrue, 1, 1)
            sh.Tags.Add "MYPICTURE", 0
        Loop Until sh.Type <> 14
    
        Dim p As Shape
        For Each p In sl.Shapes
            If p.Type = 14 Then
                If p.Tags.count > 0 Then
                    If p.Tags.Name(1) = "MYPICTURE" Then
                        p.Delete
                    End If
                End If
            End If
        Next
    End Sub
    
        2
  •  1
  •   smarty    14 年前

    一个更优雅的解决方案,用于确定占位符是否包含项,并且对于所有类型都是通用的:

    if (selectedSlide.Shapes.Placeholders[i].PlaceholderFormat.ContainedType != Microsoft.Office.Core.MsoShapeType.msoAutoShape)
    

    如果占位符中没有放置任何内容,则占位符为containedType msoAutoShape。例如,在将图像插入占位符时,类型将更改为项的类型,在本例中是msopicture。

    Office会自动将项目添加到第一个可用的占位符(在主文件中首先添加),或者对于图像,将优先处理ppPlaceholderPicture。