在 C# 中访问 Power Point 文件的文档属性

发布于 2024-12-24 18:17:09 字数 673 浏览 4 评论 0原文

我需要访问 Power Point 属性,例如作者、组织……

我该怎么做?

编辑:

这就是我正在尝试的:

static void TestProperties(Presentation presentation) // Microsoft.Office.Interop.PowerPoint.Presentation;
            {
                Microsoft.Office.Core.DocumentProperties properties;
                properties = (Microsoft.Office.Core.DocumentProperties)presentation.BuiltInDocumentProperties;

                Microsoft.Office.Core.DocumentProperty prop;

            }

这给了我 ClassCastException:

无法将类型为“System.__ComObject”的 COM 对象转换为接口类型“Microsoft.Office.Core.DocumentProperties”

我有一个文件选择器对话框,我可以在其中选择演示文稿和然后将其传递给 TestProperties 方法。

I need to access Power Point properties like Author, Organization..

How can I do that?

EDIT:

This is what I am trying:

static void TestProperties(Presentation presentation) // Microsoft.Office.Interop.PowerPoint.Presentation;
            {
                Microsoft.Office.Core.DocumentProperties properties;
                properties = (Microsoft.Office.Core.DocumentProperties)presentation.BuiltInDocumentProperties;

                Microsoft.Office.Core.DocumentProperty prop;

            }

This gives me ClassCastException:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'

I have a file chooser dialog where I choose the presentation and then pass it to TestProperties method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-12-31 18:17:09

我有一个可用的 C# 代码,可以使用 OLE 自动化(Microsoft.Office.Interop.PowerPoint):

using System;
using Microsoft.Office.Interop.PowerPoint;
using System.Reflection;

class PowerPointConverterDocumentPropertiesReaderWriter
{
    static void Main()
    {
        Application pptApplication = new Application();
        Presentation presentation = pptApplication.Presentations.Open("presentation.pptx");

        object docProperties = presentation.BuiltInDocumentProperties;
        string title = GetDocumentProperty(docProperties, "Title").ToString();
        string subject = GetDocumentProperty(docProperties, "Subject").ToString();
        string author = GetDocumentProperty(docProperties, "Author").ToString();
        string category = GetDocumentProperty(docProperties, "Category").ToString();
        string keywords = GetDocumentProperty(docProperties, "Keywords").ToString();

        SetDocumentProperty(docProperties, "Title", "new title");
        SetDocumentProperty(docProperties, "Subject", "new subject");
        SetDocumentProperty(docProperties, "Author", "new author");
        SetDocumentProperty(docProperties, "Category", "new category");
        SetDocumentProperty(docProperties, "Keywords", "new keywords");
    }

    static object GetDocumentProperty(object docProperties, string propName)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        object propValue = prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.GetProperty,
            null, prop, new object[] { });
        return propValue;
    }

    static void SetDocumentProperty(object docProperties, string propName, object propValue)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.SetProperty,
            null, prop, new object[] { propValue });
    }
}

已通过 Microsoft PowerPoint for Office 365(版本16.0)。

感谢 https://wditot。 wordpress.com/2012/05/10/extracting-metadata-from-ms-office-docs-programmatically

I have a working C# code to read / write document properties for PowerPoint documents, using OLE automation (Microsoft.Office.Interop.PowerPoint):

using System;
using Microsoft.Office.Interop.PowerPoint;
using System.Reflection;

class PowerPointConverterDocumentPropertiesReaderWriter
{
    static void Main()
    {
        Application pptApplication = new Application();
        Presentation presentation = pptApplication.Presentations.Open("presentation.pptx");

        object docProperties = presentation.BuiltInDocumentProperties;
        string title = GetDocumentProperty(docProperties, "Title").ToString();
        string subject = GetDocumentProperty(docProperties, "Subject").ToString();
        string author = GetDocumentProperty(docProperties, "Author").ToString();
        string category = GetDocumentProperty(docProperties, "Category").ToString();
        string keywords = GetDocumentProperty(docProperties, "Keywords").ToString();

        SetDocumentProperty(docProperties, "Title", "new title");
        SetDocumentProperty(docProperties, "Subject", "new subject");
        SetDocumentProperty(docProperties, "Author", "new author");
        SetDocumentProperty(docProperties, "Category", "new category");
        SetDocumentProperty(docProperties, "Keywords", "new keywords");
    }

    static object GetDocumentProperty(object docProperties, string propName)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        object propValue = prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.GetProperty,
            null, prop, new object[] { });
        return propValue;
    }

    static void SetDocumentProperty(object docProperties, string propName, object propValue)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.SetProperty,
            null, prop, new object[] { propValue });
    }
}

Tested and works as expected with Microsoft PowerPoint for Office 365 (version 16.0).

Thanks to https://wditot.wordpress.com/2012/05/10/extracting-metadata-from-ms-office-docs-programmatically.

陪你到最终 2024-12-31 18:17:09

这样的事情对你有用吗..?更改属性[“”]以适合您的情况

Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
    Globals.ThisWorkbook.BuiltinDocumentProperties; 
Microsoft.Office.Core.DocumentProperty prop;
prop = properties["Revision Number"]; 

Will something like this work for you..? change the properties[" "] to fit your case

Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
    Globals.ThisWorkbook.BuiltinDocumentProperties; 
Microsoft.Office.Core.DocumentProperty prop;
prop = properties["Revision Number"]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文