在PowerPoint 2007中并使用VBA,如何在Slide Master布局上获取占位符形状,该布局是幻灯片上占位符形状的“主”?
我目前正在使用循环来比较幻灯片占位符的位置和大小以及幻灯片布局中每个占位符形状的位置和形状,但这不是万无一失的。例如,如果在幻灯片上移动占位符形状,则其位置可能不再与幻灯片布局中任何占位符形状的位置匹配。我可以重新应用幻灯片的布局来将占位符重新放回原位,但这不是我想要做的。
像Shape.Master
这样的对象模型中的某些东西是理想的,当然,这不存在。
答案 0 :(得分:1)
看到这里或其他地方仍然没有答案我也可以发布我的代码。
例如,如果在幻灯片上移动占位符形状,
以下是我提出的处理方法:
这是执行此操作并返回mastershapename - shapename映射的函数。
private Dictionary<string, string> GetShapeMasters(Powerpoint.Slide s)
{
Dictionary<string, string> shapeMasters = new Dictionary<string, string>();
List<ShapeLocation> shapeLocations = new List<ShapeLocation>();
//store locations
foreach (Powerpoint.Shape sh in s.Shapes)
{
shapeLocations.Add(new ShapeLocation()
{
Name = sh.Name,
Location = new System.Drawing.RectangleF(sh.Left, sh.Top, sh.Width, sh.Height)
});
}
//have powerpoint reset the slide
//ISSUE: this changes the names of placeholders without content.
s.CustomLayout = s.CustomLayout;
//compare slide and master
foreach (Powerpoint.Shape sh in s.Shapes)
{
foreach (Powerpoint.Shape msh in s.CustomLayout.Shapes)
{
if (IsShapeMaster(sh, msh))
{
shapeMasters[msh.Name] = sh.Name;
}
}
}
//restore locations
//TODO: might be replaced by undo
foreach (var shm in shapeLocations)
{
Powerpoint.Shape sh = null;
try
{
sh = s.Shapes[shm.Name];
}
catch
{
//Fails for renamed placeholder shapes.
//Have yet to find a decent way to check if a shape name exists.
}
//placeholders do not need to be restored anyway.
if (sh != null)
{
sh.Left = shm.Location.Left;
sh.Top = shm.Location.Top;
sh.Width = shm.Location.Width;
sh.Height = shm.Location.Height;
}
}
return shapeMasters;
}
这样你可以做到
Dictionary<string, string> shapeMasters = GetShapeMasters(theSlide);
if(shapeMasters.ContainsKey("KnownPlaceholderName"))
Powerpoint.Shape KnownShape = theSlide[shapeMasters["KnownPlaceholderName"]];
这是比较函数,它采用两种形状并检查它们是否“相等”。可以扩展以使其更精确。
private bool IsShapeMaster(Powerpoint.Shape sh, Powerpoint.Shape msh)
{
return
sh.Left == msh.Left
&& sh.Top == msh.Top
&& sh.Width == msh.Width
&& sh.Height == msh.Height
&& sh.Type == msh.Type
&& sh.PlaceholderFormat.Type == msh.PlaceholderFormat.Type;
}
存储原始形状位置的小类
class ShapeLocation
{
public string Name;
public System.Drawing.RectangleF Location;
}
这是来自C#VSTO加载项的代码,但我认为它与VB或其他PPT自动化类型没有什么不同。
答案 1 :(得分:0)
你去Ryan,我相信这就是你所要求的。
Sub GetLayoutShapeDetails()
Dim myPPT As Presentation
Set myPPT = ActivePresentation
Dim mySlide As Slide
Set mySlide = myPPT.Slides(6)
Dim slideShape As Shape
Dim slideLayoutShape As Shape
Set slideShape = mySlide.Shapes(1)
If slideShape.Type = msoPlaceholder Then
Dim placeHolderType As Integer
placeHolderType = slideShape.PlaceholderFormat.Type
Set slideLayoutShape = mySlide.CustomLayout.Shapes.Placeholders(placeHolderType)
Dim modifiedPlaceHolder As String
modifiedPlaceHolder = "Shape Name: " & slideShape.Name & _
", Left: " & slideShape.Left & _
", Width: " & slideShape.Width
Dim originalPlaceHolder As String
originalPlaceHolder = "Shape Name: " & slideLayoutShape.Name & _
", Left: " & slideLayoutShape.Left & _
", Width: " & slideLayoutShape.Width
Debug.Print modifiedPlaceHolder
Debug.Print originalPlaceHolder
End If
End Sub
编辑: 2010年1月16日 基于进一步的研究,VBA中似乎没有办法在幻灯片的布局中找到形状相应的完全匹配。