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:消息:演示(未知成员):无效请求。不授予自动化权限。 StackTrace:位于 Microsoft.Office.Interop.PowerPoint._Presentation.get_Name()
这是我所知道的Presentations.Count是1并且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 资源管理器中选择启用了预览 Pane 的演示文稿时,Windows 资源管理器似乎会在隐藏窗口中打开该演示文稿。如果您尝试从 COM 加载项访问任何隐藏演示文稿的对象成员(例如,ppPresentation.Name
),您将收到“未授予自动化权限”的信息。异常。
不幸的是,似乎没有一个好的方法来确定隐藏的演示文稿是否由 Windows 资源管理器打开(例如,ppPresentation.Windows.Count = 0
),因为访问演示文稿的任何通过代码的对象成员似乎会抛出此异常。因此,唯一的解决方法似乎是错误处理,例如 Try/Catch
。
请注意,Presentations.Count
返回所有打开的演示文稿的数量,包括通过预览 Pane 打开的演示文稿,因此如果您的添加 -依赖于它实际可以使用的演示文稿的准确计数。
另请注意,此问题似乎不会以同样的方式影响 Excel。
关于c# - 资源管理器预览导致 ActivePresentation.name 上出现 System.Runtime.InteropServices.COMException : Automation rights are not granted.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38601251/