3

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)
            {
            }
        }

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Globals.ThisAddIn.Application.ActiveWindow.Selection - indeed :) You've made my day :) Thanks a lot! Could you also take a look at another problem I'm dealing with? : stackoverflow.com/questions/8993376/…
Been there. Done that. Wearing the "Tags! Respect Them!" t-shirt.
It's all good until you get this exception while doing it - Selection (unknown member) : Invalid request. Nothing appropriate is currently selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.