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

时间:2012-01-03 14:58:02

标签: c# properties document powerpoint

我需要访问Power Point属性,例如Author,Organization ..

我该怎么做?

编辑:

这就是我的尝试:

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方法。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我有一个有效的C#代码,用于使用OLE自动化(Microsoft.Office.Interop.PowerPoint 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