c# - 资源管理器预览导致 ActivePresentation.name 上出现 System.Runtime.InteropServices.COMException : Automation rights are not granted.

标签 c# .net com vsto powerpoint

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

看来我不是唯一一个在资源管理器预览中遇到此问题的人:

听起来这是一个权限问题?希望它像设置 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/

相关文章:

c# - Selenium 查找指定元素内的元素

C# 检查文件的二进制读取器结尾

java - SonarQube 5.6 是否执行使用 sonar-plugin-api 版本 4.5.2 开发的插件中的装饰器?

.net - MouseDown 事件期间 e.X 和 MousePosition.X 之间的区别?

.net - 从 .NET 中的 10 000 个项目自动完成

c# - 可选地使用可能未注册的 COM 对象

c# - SqlDataAdapter输出变量问题C#

c# - 是否可以在 winforms 应用程序中定义左手组合框(即左侧的滚动条)?

c++ - Outlook 2010 C++ 加载项 - HTML 电子邮件正文检索

windows - 在所有未使用的情况下,进程外 COM 服务器最终是否应该停止?