Reputation: 17773
Is there a way to access currently active slide in PowerPoint presentation using VSTO? Also is would be nice if I could get currently active Shape. I know how to iterate through slides / shapes, yet I can't find any property to figure out whether slide/shape is active:
foreach (Slide slide in presentation.Slides)
{
foreach (Shape shape in slide.Shapes)
{
}
}
Upvotes: 3
Views: 5125
Reputation: 3528
Look at the .Selection object.
It has a .Type property that tells you what's selected. If it's SlideRange, the selection might be one or more slides; up to you to decide what to do if > 1, but if 1, then .Selection.SlideRange(1) gives you a reference to the selected slide.
.Type might return ShapeRange, in which case you'd use .Selection.ShapeRange(1) to get the current shape or the first shape in the range if more than one shape is selected. The shape's .Parent property returns a reference to the slide the shape is on (slide, master, layout, whatever).
If .Type returns TextRange, you have to walk a few steps up the parent chain; the parent of text is textrange, the parent of textrange is the containing shape and the shape's parent is the slide the shape is on.
This is liable to fall apart in some versions of PowerPoint 2007 (it's broken pre SP1 or 2, as I recall), and it's still broken in 2010 when the text in a table is selected. You can manipulate the text, you can get access to some of the properties of the shape that contains the text, but not all, and you can't climb the .Parent ladder to the slide.
Upvotes: 10