隐藏

C#解析读取Word文档详细信息属性

发布:2020/2/4 20:05:06作者:管理员 来源:本站 浏览次数:1027

using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Core;

public static string readWordFileKey(string file)
        {
            string strkey = "";
            try
            {
                Microsoft.Office.Interop.Word.Application wordApp;                   //Word应用程序变量 
                wordApp = new Microsoft.Office.Interop.Word.Application(); //初始化
                wordApp.Visible = false;
                Microsoft.Office.Interop.Word.Document wordDoc;                  //Word文档变量
                
                object owordpath = file;
                object missing = System.Reflection.Missing.Value;
                object vreadonly = true;
                object vrevert = false;
                object vvisible = false;
                wordDoc = wordApp.Documents.Open(ref owordpath, ref missing, ref vreadonly,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing,
                    ref missing, ref missing, ref missing);
                
                strkey = wordDoc.Application.ActiveDocument.BuiltInDocumentProperties[WdBuiltInProperty.wdPropertyKeywords].Value;

                object nosave = false;
                wordDoc.Close(ref nosave, ref missing, ref missing);

                wordApp.Quit();

            }
            catch (Exception e)
            {
                //  MessageBox.Show(e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            }
            return strkey;
        }