日期:2014-05-17  浏览次数:21026 次

C# 操作 Word 修改word的高级属性中的自定义属性

 

 

word的类库使用的是word2007版本的类库,类库信息见下面图片,折腾了半天,终于找到入口,网上 很多说的添加或者修改word的高级属性中的自定义属性都是错误的,感觉都是在copy网上的代码,自己终于摸索成功了,Mark下。

 

直接上代码,代码如下:

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

namespace TestWord
{
    /// <summary>
    /// 从传入的word文档中替换掉指定书签的内容,主要是为了解决封面中的内容
    /// 抽取命令:
    /// xx.exe -p D:/temp/test.doc D:/result/mappting.txt
    /// 
    /// mappting.txt 文件是替换test.doc文档中标签的匹配文件,比如哪个标签的内容需要替换为其他的值
    /// </summary>
    class PageExtractor : IExtractor
    {

        private object oMissing = System.Reflection.Missing.Value;

        //标准页眉要替换的值在mapping.txt文件中存储的key
        private String TB_YEMEI_GJB = "TB_YEMEI_GJB";

        //写入word的高级属性中的内容,需要设置为整形的高级属性名
        private String[] m_oIntegerAdvPropertyNames = new String[]{ "密级","稿件"};

        //写入word的高级属性中的内容,需要设置为boolean
        private String[] m_oBoolAdvPropertyNames = new String[] { "TRS" };

        /// <summary>
        /// 从指定word中抽取出word正文
        /// </summary>
        /// <param name="_sSourceWordPath"></param>
        /// <returns></returns>
        public bool execute(string[] args)
        {
            _Application oWord = WordProcessHelper.getInstance().getApplication();
            _Document oDoc = null;
            String _sWordPath = "";
            LogServer currLogServer = LogServer.getInstance();

            try
            {
                //1.参数需要长度为3,第一个是“-p”,第二个是“要抽取的word路径”,第三个是“匹配的书签替换文件”
                if (args.Length < 3)
                {
                    throw new Exception("替换封面时传入的参数个数至少为3个,第一个是‘-p’,第二个是‘要抽取的word路径’,第三个是标签‘匹配的UTF-8编码的txt文件内容’,第四个是文档的高级属性内容");
                }

                _sWordPath = args[1];//获取要抽取的原word路径
                if (!File.Exists(_sWordPath))
                {
                    throw new Exception("Word文件[" + _sWordPath + "]不存在!");
                }

                String sMappingFilePath = args[2];
                if (CMyString.isEmpty(sMappingFilePath))
                {
                    throw new Exception("没有传入替换doc文件内容的匹配文件路径!");
                }

                //1.读取需要替换的文件内容
                Properties mappingProperties = new Properties(sMappingFilePath);
                if (mappingProperties.size() <= 0)
                {
                    currLogServer.writeLogLine("文件[" + sMappingFilePath + "]中要替换的内容为空!");
                    return true;
                }

                //2.读取word中标签
                DateTime beginDateTime = new DateTime();
                object oDocFilePath = _sWordPath;
                object oReadOnly = false;
                object oAddToRecentFiles = false;
                oDoc = oWord.Documents.Open(ref oDocFilePath, ref oMissing, ref oReadOnly, ref oAddToRecentFiles, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                //3.获取标签名与对应的Range对象的对应关系
                Hashtable oBookIdToRangeMap = getBookIdToRange(oDoc, mappingProperties);
                if (oBookIdToRangeMap == null || (oBookIdToRangeMap.Count == 0))
                {
                    return true;
                }

                //4.替换Range对象的内容为mapping文件中的属性值
                replaceMappingContent(oDoc, oBookIdToRangeMap, mappingProperties);

                //5.替换页眉
                Range allRange = oDoc.Range(ref