In my PowerPoint AddIn I want to access shapes on the slides. The shapes are placeholders defined in custom layouts in the slidemaster.
When I add a slide based on the custom layout, the shapes just get named "placeholder 1", "placeholder 2", ...
Is there a way to get the placeholder by the name given in the master?
Currently I am searching shapes with this code:
public static Shape GetShape(string stringToSearch, Shapes shapes) {
foreach (Shape shape in shapes) {
if (shape.Name == stringToSearch) {
return shape;
}
// Search Groups
if (shape.Type == MsoShapeType.msoGroup) {
foreach (Shape childshape in shape.GroupItems) {
if (childshape.Name == stringToSearch) {
return childshape;
}
}
}
}
throw new KeyNotFoundException("No Shape found");
}
Update: Maybe to make it more clear, this is the structure of the PowerPoint-Presentation.
Master with Names defined for placeholders:
Presentation where names defined in master are lost:
Problem: How to get element in presentation, by name defined in master?
Well.. here is Steve's "Ugly!" solution.
For my project I neither have nor want control over creation of shapes so I cannot "tag" them. That is why I have a custom placeholder name to identify them, So the names of shapes will indeed be [PlaceholderType] ##.
The steps:
Note: I don't use shape groups. this technique will get a lot more complicated if you do and you need to check inside groups.
This is the function that does that and returns a mastershapename - shapename mapping.
With this you can do
And here is the comparison function that takes two shapes and checks if they are "equal". Could be extended to make it more precise.
Little class that stores original shape location
I'm open to suggestions because I don't like this, either. Only there seems to be no other way to link shapes and placeholders together. There really isn't some
shape.MasterShape
we are missing, is there?