JDR
JDR

Reputation: 1144

C# PowerPoint VSTO: Check if Any Slide is in View

PowerPoint's interop library exposes Globals.ThisAddIn.Application.ActiveWindow.View.Slide which allows you to test if a slide (any Slide or Master) is currently selected and in the view.

If no slide is currently in the view, then the View property is null. But here's the twist: you are unable to check ActiveWindow.View for null, without raising an exception.

My question is this:

How do you check if a slide/master is currently selected without resorting to an ugly try/catch like the following?

    internal static bool SlideActive => Slide != null;

    internal static dynamic Slide
    {
        get
        {
            try
            {
                return Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
            }
            catch
            {
                return null;
            }
        }
    }

EDIT:

This is the exception thrown when accessing Slide if none is in view:

{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.Runtime.InteropServices.COMException: View.Slide : Invalid request.  No slide is currently in view.

Upvotes: 2

Views: 928

Answers (1)

JDR
JDR

Reputation: 1144

I found a solution to this problem which involves checking the ActiveWindow's Pane's Active property.

A method returning either the active Slide/Master or null could look as follows - no try/catch required:

    internal static dynamic CurrentSlide
    {
        get
        {
            if (Globals.ThisAddIn.Application.Active == MsoTriState.msoTrue &&
                Globals.ThisAddIn.Application.ActiveWindow.Panes[2].Active == MsoTriState.msoTrue)
            {
                return Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
            }
            return null;
        }
    }

First, we need to check if the application is active, followed by checking if the thumbnail pane's corresponding Pane is active (to account for the user having de-selected all slides/masters) and, finally, returning our object or null.

Props go out to this person having gotten me onto the right track.

Upvotes: 3

Related Questions