gpt4 book ai didi

c# - 在 C# 中列出 word 文档的属性

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:46 26 4
gpt4 key购买 nike

我需要一些帮助来检索 word 文档的属性。我已经知道标题、主题和作者了。但我似乎无法获取“上次保存日期”属性,而且我不知道如何获取属性名称列表。

我的代码是这样的:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop;
using System.Reflection;
using System.IO;


namespace MetaDataSorter
{
class Program
{

static void Main(string[] args)
{
String dirName = @"H:\projekt\test raw files";

String fileNameString = @"H:\projekt\raw files\vgahm\1 NTFS\Raw Files\Microsoft Word Document\1-300\FILE006.DOC";
object fileName = (object)fileNameString;

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();


Microsoft.Office.Interop.Word.Document aDoc = null;

if (File.Exists((string)fileName))
{
DateTime toDay = DateTime.Now;

object readOnly = false;
object isVisible = false;

wordApp.Visible = false;

aDoc = wordApp.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);

aDoc.Activate();

//object property = getWordDocumentPropertyValue(aDoc, "Title");

System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Title"));
System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Subject"));
System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Author"));
//System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Date Last Saved"));

aDoc.Close();
}

}

private static String getWordDocumentPropertyValue(Microsoft.Office.Interop.Word.Document document, string propertyName)
{
object builtInProperties = document.BuiltInDocumentProperties;

Type builtInPropertiesType = builtInProperties.GetType();

object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });

Type propertyType = property.GetType();
object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
return propertyValue.ToString();
}

}
}

我应该如何获取属性值列表?

最佳答案

您可以先查看类 BuiltInDocumentProperties 或使用此链接作为起点,http://support.microsoft.com/default.aspx?scid=kb;en-us;303294

请记住,有些属性是办公套件中某些产品特有的,有些是通用的。您要找的肯定是普通的。

关于word,这里找列表http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.wdbuiltinproperty%28v=office.11%29.aspx

关于c# - 在 C# 中列出 word 文档的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com