获取word文档的作者和主题
2014-09-11来源:易贤网

当然要先添加对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);

}

更多信息请查看IT技术专栏

推荐信息