獲取word文檔的作者和主題

字號:


    當(dāng)然要先添加對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
    {
    //關(guān)閉word進程
    object save = false;
    oword.quit(ref save,ref unknown,ref unknown);
    system.runtime.interopservices.marshal.releasecomobject(oword);
    }