资源管理器预览原因 System.Runtime.InteropServices.COMException:未授予自动化权限。在 ActivePresentation.name
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;
}
}
我这样调用这个函数:
PowerPoint.Presentation ppPresentation = PowerPointApplication.GetActivePPT();
if(ppPresentation != null)
{
Console.WriteLine(ppPresentation.Name);
}
然后我得到一个:
COMException:Message:Presentation(未知成员):无效请求。未授予自动化权限。 StackTrace:在 Microsoft.Office.Interop.PowerPoint._Presentation.get_Name()
这是我所知道的 Presentations.Count 是一个并且 application.ActivePresentation 不是 null
看来我不是唯一一个在浏览器预览中遇到此问题的人:
- 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
听起来这是权限问题?希望它像设置 COMVisible(true) 一样简单,但目前没有好主意。
this blog post 似乎声称写锁在起作用,但 Word 和 Excel 没有表现出相同的行为。
当您在 Windows 资源管理器中 select 启用了预览窗格的演示文稿时,Windows 资源管理器似乎在隐藏的 window 中打开该演示文稿。如果您尝试从 COM 加载项访问任何隐藏的演示文稿的对象成员(例如,ppPresentation.Name
),您会得到 "Automation rights are not granted." 异常。
不幸的是,似乎没有一个好的方法来确定隐藏的演示文稿是否被 Windows Explorer(例如,ppPresentation.Windows.Count = 0
)打开,因为通过访问任何演示文稿的对象成员代码似乎抛出这个异常。因此,唯一的解决方法似乎是错误处理,例如 Try/Catch
.
请注意 Presentations.Count
returns 所有 打开的演示文稿的数量,包括通过预览窗格打开的演示文稿,因此您需要考虑到这一点如果您的加载项依赖于准确的演示文稿计数,它实际上可以使用。
另请注意,此问题似乎不会以同样的方式影响 Excel。
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;
}
}
我这样调用这个函数:
PowerPoint.Presentation ppPresentation = PowerPointApplication.GetActivePPT();
if(ppPresentation != null)
{
Console.WriteLine(ppPresentation.Name);
}
然后我得到一个:
COMException:Message:Presentation(未知成员):无效请求。未授予自动化权限。 StackTrace:在 Microsoft.Office.Interop.PowerPoint._Presentation.get_Name()
这是我所知道的 Presentations.Count 是一个并且 application.ActivePresentation 不是 null
看来我不是唯一一个在浏览器预览中遇到此问题的人:
- 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
听起来这是权限问题?希望它像设置 COMVisible(true) 一样简单,但目前没有好主意。
this blog post 似乎声称写锁在起作用,但 Word 和 Excel 没有表现出相同的行为。
当您在 Windows 资源管理器中 select 启用了预览窗格的演示文稿时,Windows 资源管理器似乎在隐藏的 window 中打开该演示文稿。如果您尝试从 COM 加载项访问任何隐藏的演示文稿的对象成员(例如,ppPresentation.Name
),您会得到 "Automation rights are not granted." 异常。
不幸的是,似乎没有一个好的方法来确定隐藏的演示文稿是否被 Windows Explorer(例如,ppPresentation.Windows.Count = 0
)打开,因为通过访问任何演示文稿的对象成员代码似乎抛出这个异常。因此,唯一的解决方法似乎是错误处理,例如 Try/Catch
.
请注意 Presentations.Count
returns 所有 打开的演示文稿的数量,包括通过预览窗格打开的演示文稿,因此您需要考虑到这一点如果您的加载项依赖于准确的演示文稿计数,它实际上可以使用。
另请注意,此问题似乎不会以同样的方式影响 Excel。