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

a 夏天 提交于 2020-12-06 06:33:30

问题


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:

  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/327cfc7b-07a3-49ad-9e0b-f7100852e3bf/applicationpresentationsopen-generating-exception-error-code-2147467259-automation-rights-are?forum=vsto
  • https://social.msdn.microsoft.com/Forums/en-US/e7437e44-1aea-4ab5-bbf2-6794037c872a/vsto-powerpoint-explorer-previewpane?forum=vsto
  • http://youpresent.co.uk/presentations-count-returns-wrong-number/
  • https://github.com/jon-hedgerows/msofficesvn/issues/25
  • https://groups.google.com/forum/#!topic/microsoft.public.powerpoint/KR1VuXtDccQ

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.


回答1:


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.



来源:https://stackoverflow.com/questions/38601251/explorer-preview-causes-system-runtime-interopservices-comexception-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!