Search code examples
c#vstopowerpoint

How to test for PowerPoint SlideShowWindow object?


I am automating PowerPoint 2010 using VSTO and I want to check if the SlideShowWindow is available before calling methods on it.

At the moment I am catching the COMException returned when accessing;

Globals.ThisAddIn.Application.ActivePresentation.SlideShowWindow

The full method is;

private SlideShowWindow GetSlideShowWindow()
{
    //attempt to get the running slide show window...
    SlideShowWindow slideShowWindow = null;
    try
    {
        //try to access the COM wrapper...
        slideShowWindow = Globals.ThisAddIn.Application.ActivePresentation.SlideShowWindow;
    }
    catch (COMException)
    {
        //window doesn't exist!...
    }

    //return window or null...
    return slideShowWindow;
}

It seems like there should be an enum or flag somewhere in the object model that would avoid this approach?


Solution

  • Not sure what the comment about Open XML is about, that is an unrendered content manipulation tool. What you're trying to do is correctly assumed to be programmed by the object model.

    Here's what I would do:

    Public Sub SeeIfAShowIsRunning()
        If SlideShowWindows.Count > 0 Then
            Debug.Print "yep, something is there. Let me check further."
            If SlideShowWindows.Count > 1 Then
            Debug.Print "uh huh, there are " & SlideShowWindows.Count & " shows running"
            Debug.Print "hold on, i'll figure this out for you"
                For i = 1 To SlideShowWindows.Count
                    If ActivePresentation.Name = Presentations.Item(i).Name Then
                        Debug.Print "i found you. your name is: " & ActivePresentation.Name
                    Else
                        Debug.Print Presentations.Item(i).Name & " is a fine pptx, but not the one i want"
                    End If
                Next
            End If
        Else
            Debug.Print "nope, you're in editing mode"
        End If
    End Sub
    

    This will work in all versions of PowerPoint. In PowerPoint 2010, you can technically have more than one slide show window running, but only one of them would be active, so I would do a further search for the active one and get it's .Presentation and if it matches ActivePresentation, then it is your ActivePresentation's slide show that is running, like above.

    If your using PowerPoint 2007 or below, then this isn't an issue and you can remove the If SlideShowWindows.Count > 1 Then statement.