Search code examples
c#.netcomvstopowerpoint

explorer preview causes System.Runtime.InteropServices.COMException: Automation rights are not granted. on ActivePresentation.name


public static PowerPoint.Presentation GetActivePPT(this PowerPoint.Application application)
{   
            try
            {
                if (App.Presentations.Count > 0)
                {
                    return application.ActivePresentation;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
}

I call this function like so:

PowerPoint.Presentation ppPresentation = PowerPointApplication.GetActivePPT();
if(ppPresentation != null)
{
   Console.WriteLine(ppPresentation.Name);
}

And I get a :

COMException: Message:Presentation (unknown member) : Invalid request. Automation rights are not granted. StackTrace: at Microsoft.Office.Interop.PowerPoint._Presentation.get_Name()

Here is what I know Presentations.Count is one and application.ActivePresentation is not null

It looks like I am not the only one to encounter this issue with Explorer preview:

It sounds like this is a permissions issue? hoping its as simple as setting something to COMVisible(true) but no good ideas at this point.

this blog post seems to claim a write lock is in play but Word and Excel do not exhibit the same behavior.


Solution

  • When you select a presentation in Windows Explorer with preview pane enabled, Windows Explorer appears to open the presentation in a hidden window. If you attempt to access any of the hidden presentation's object members (e.g., ppPresentation.Name) from a COM add-in you get the "Automation rights are not granted." exception.

    Unfortunately, there does not appear to be a good way to determine if the hidden presentation was opened by Windows Explorer (e.g, ppPresentation.Windows.Count = 0), since accessing any of the presentation's object members via code seems to throw this exception. Therefore, the only workaround appears to be error handling, like Try/Catch.

    Note that Presentations.Count returns the number of all open presentations, including those opened by the preview pane, so you will need to account for this if your add-in relies on an accurate count of presentations it can actually work with.

    Also, note that this problem does not appear to affect Excel in this same way.