根据幻灯片,如何判断所有幻灯片布局占位符是否在PowerPoint中使用?
如果没有使用占位符,可以在添加图片时阻止自动使用占位符吗?
答案 0 :(得分:1)
您需要遍历幻灯片上的所有占位符,确定每个占位符,然后检查它是否填充了预期的格式。有18个 PpPlaceholderType
,因此您必须将它们全部设置完毕,但以下是您可以检查是否正在使用占位符的示例。< / p>
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
答案 1 :(得分:1)
更优雅的解决方案,以确定占位符是否包含项目,并且对于所有类型都是通用的:
if (selectedSlide.Shapes.Placeholders[i].PlaceholderFormat.ContainedType != Microsoft.Office.Core.MsoShapeType.msoAutoShape)
如果占位符内没有放置任何内容,则占位符为ContainedType msoAutoShape。将例如图像插入占位符时,类型会更改为项目类型,在本例中为msoPicture。
Office会自动将项目添加到第一个可用的占位符(在主文件中首先添加),或者为图像优先处理ppPlaceholderPicture。