获取Word文档的作者和主题

2024-04-30 162 0

当然要先添加对Word的组件的引用,添加命名空间的引用,代码:

Word.Application oWord;

Word._Document oDoc;

object oMissing = Missing.Value;

object docBuiltInProps;

object Source = @"DocPath";

object Unknown =Type.Missing;

oWord = new Word.Application();

try

{

oDoc = oWord.Documents.Open(ref Source,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown );

docBuiltInProps = oDoc.BuiltInDocumentProperties;

Type typeDocBuiltInProps = docBuiltInProps.GetType();

//获取作者

string index = "Author";

string propsValue;

object docAuthor = typeDocBuiltInProps.InvokeMember("Item",

BindingFlags.Default |

BindingFlags.GetProperty,

null,docBuiltInProps,

new object[] {index} );

Type typeDocAuthorProp = docAuthor.GetType();

propsValue = typeDocAuthorProp.InvokeMember("Value",

BindingFlags.Default |

BindingFlags.GetProperty,

null,docAuthor,

new object[] {} ).ToString();

MessageBox.Show( propsValue,"Author" );

//获取主题

index = "Subject";

propsValue = "The Subject";

object docSubjectProp = typeDocBuiltInProps.InvokeMember("Item",

BindingFlags.Default |

BindingFlags.GetProperty,

null,docBuiltInProps,

new object[] {index} );

Type typeDocSubjectProp = docSubjectProp.GetType();

propsValue = typeDocSubjectProp.InvokeMember("Value",

BindingFlags.Default |BindingFlags.GetProperty,

null,docSubjectProp,

new object[] {} ).ToString();

MessageBox.Show( propsValue,"Subject" );

}

finally

{

//关闭word进程

object save = false;

oWord.Quit(ref save,ref Unknown,ref Unknown);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);

}

,办公群:185219299

相关文章

Switch怎样限制孩子下载的游戏类型
如何批量修改Word文档正文字体格式
win10设置新密码总是不符合
如何解决win10系统开机速度慢问题
win10休眠在哪里
win10如何更**辨率?