

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
- {
-
-
-
-
-
-
-
- class PageExtractor : IExtractor
- {
-
- private object oMissing = System.Reflection.Missing.Value;
-
-
- private String TB_YEMEI_GJB = "TB_YEMEI_GJB";
-
-
- private String[] m_oIntegerAdvPropertyNames = new String[]{ "密级","稿件"};
-
-
- private String[] m_oBoolAdvPropertyNames = new String[] { "TRS" };
-
-
-
-
-
-
- public bool execute(string[] args)
- {
- _Application oWord = WordProcessHelper.getInstance().getApplication();
- _Document oDoc = null;
- String _sWordPath = "";
- LogServer currLogServer = LogServer.getInstance();
-
- try
- {
-
- if (args.Length < 3)
- {
- throw new Exception("替换封面时传入的参数个数至少为3个,第一个是‘-p’,第二个是‘要抽取的word路径’,第三个是标签‘匹配的UTF-8编码的txt文件内容’,第四个是文档的高级属性内容");
- }
-
- _sWordPath = args[1];
- if (!File.Exists(_sWordPath))
- {
- throw new Exception("Word文件[" + _sWordPath + "]不存在!");
- }
-
- String sMappingFilePath = args[2];
- if (CMyString.isEmpty(sMappingFilePath))
- {
- throw new Exception("没有传入替换doc文件内容的匹配文件路径!");
- }
-
-
- Properties mappingProperties = new Properties(sMappingFilePath);
- if (mappingProperties.size() <= 0)
- {
- currLogServer.writeLogLine("文件[" + sMappingFilePath + "]中要替换的内容为空!");
- return true;
- }
-
-
- 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);
-
-
- Hashtable oBookIdToRangeMap = getBookIdToRange(oDoc, mappingProperties);
- if (oBookIdToRangeMap == null || (oBookIdToRangeMap.Count == 0))
- {
- return true;
- }
-
-
- replaceMappingContent(oDoc, oBookIdToRangeMap, mappingProperties);
-
-
- Range allRange = oDoc.Range(ref oMissing, ref oMissing);
- Sections allSections = allRange.Sections;
- currLogServer.writeLogLine("开始替换[" + _sWordPath + "]页眉.");
-
- if (allSections != null && (allSections.Count > 0))
- {
- String sTBYeMeiGJB = mappingProperties.getPropertyAsString(TB_YEMEI_GJB, false);
- currLogServer.writeLogLine("[" + _sWordPath + "]页眉要替换的值为:" + sTBYeMeiGJB);
- sTBYeMeiGJB = CMyString.showEmpty(sTBYeMeiGJB);
- for (int i = 1; i <= allSections.Count; i++)
- {
- Section aSection = allSections[i];
- if (aSection == null)
- {
- continue;
- }
-
-
- aSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = sTBYeMeiGJB;
- aSection.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.Text = sTBYeMeiGJB;
- }
- }
-
- DateTime endDateTime = new DateTime();
- String sTimeDuration = DateTimeHelper.dateDiff(beginDateTime, endDateTime);
- currLogServer.writeLogLine("[" + _sWordPath + "]页眉替换完成,用时:" + sTimeDuration);
-
-
-
- if (args.Length < 4) {
- return true;
- }
-
- String sAdvancePropertyFilePath=args[3];
- if (CMyString.isEmpty(sAdvancePropertyFilePath)) {
- return true;
- }
- Properties advanceProperties = new Properties(sAdvancePropertyFilePath);
- if (advanceProperties.size() <= 0)
- {
- currLogServer.writeLogLine("文件[" + sAdvancePropertyFilePath + "]中要写入的高级属性内容为空!");
- return true;
- }
-
-
- currLogServer.writeLogLine("[" + _sWordPath + "]开始写入高级属性.");
- writeDocAdvanceProperty(oDoc, advanceProperties);
- currLogServer.writeLogLine("[" + _sWordPath + "]高级属性写入完成.");
-
-
- return true;
-
- }
- catch (Exception ex)
- {
- currLogServer.writeLogLine("页眉替换从文件[" + _sWordPath + "]时发生错误:"+ex.Message);
- throw new Exception("页眉替换从文件[" + _sWordPath + "]时发生错误", ex);
- }
- finally
- {
-
- object saveOption = WdSaveOptions.wdSaveChanges;
- if (oDoc != null)
- {
- oDoc.Close(ref saveOption, ref oMissing, ref oMissing);
-
- }
-
- }
- }
-
-
-
-
-
-
- private void writeDocAdvanceProperty(_Document oDoc, Properties advanceProperties) {
- if (oDoc == null || (advanceProperties == null) || (advanceProperties.size()<=0))
- {
- return;
- }
-
-
- Hashtable allAdvanceProperties=advanceProperties.getProperties();
- if (allAdvanceProperties == null || (allAdvanceProperties.Count <= 0)) {
- return;
- }
-
-
- object oDocCustomProps = oDoc.CustomDocumentProperties;
- Type typeDocCustomProps = oDocCustomProps.GetType();
-
-
- foreach (String sPropName in allAdvanceProperties.Keys)
- {
- String sValue = (String)allAdvanceProperties[sPropName];
- sValue = CMyString.showEmpty(sValue);
-
-
- Object oPropItemObj = getPropertyObjByName(oDocCustomProps, typeDocCustomProps, sPropName);
- if (oPropItemObj == null)
- {
-
- MsoDocProperties oPropertType = getPropertyTypeByName(sPropName);
- object[] oArgs = {sPropName,false,
- oPropertType,
- sValue};
- typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
- BindingFlags.InvokeMethod, null,
- oDocCustomProps, oArgs);
- }
- else {
-
- Type typeItemProp = oPropItemObj.GetType();
- typeItemProp.InvokeMember("Value",
- BindingFlags.Default | BindingFlags.SetProperty,
- null, oPropItemObj,
- new object[] { sValue });
- }
- }
-
- }
-
-
-
-
-
-
-
-
- private object getPropertyObjByName(object oDocCustomProps,Type typeDocCustomProps, String _sPropertyName)
- {
- if (CMyString.isEmpty(_sPropertyName)) {
- return null;
- }
- if (oDocCustomProps == null || typeDocCustomProps == null) {
- return null;
- }
-
-
-
- object oPropItem = null;
- try
- {
- oPropItem = typeDocCustomProps.InvokeMember("Item",
- BindingFlags.Default |
- BindingFlags.GetProperty,
- null, oDocCustomProps,
- new object[] { _sPropertyName });
- }
- catch (Exception ex) {
-
- oPropItem = null;
- }
-
-
- return oPropItem;
- }
-
-
-
-
-
-
- private MsoDocProperties getPropertyTypeByName(String sPropName)
- {
- if (CMyString.isEmpty(sPropName)) {
- return MsoDocProperties.msoPropertyTypeString;
- }
-
- bool bInStrArray = CMyList.isInStrArray(sPropName, m_oIntegerAdvPropertyNames, true);
- if (bInStrArray) {
- return MsoDocProperties.msoPropertyTypeNumber;
- }
-
- bInStrArray = CMyList.isInStrArray(sPropName, m_oBoolAdvPropertyNames, true);
- if (bInStrArray) {
- return MsoDocProperties.msoPropertyTypeBoolean;
- }
-
-
- return MsoDocProperties.msoPropertyTypeString;
- }
-
-
-
-
-
-
-
- private Hashtable getBookIdToRange(_Document oDoc, Properties mappingProperties) {
-
- Hashtable oBookIdToRangeMap = new Hashtable();
- if (oDoc == null || (mappingProperties == null)) {
- return oBookIdToRangeMap;
- }
-
-
- Bookmarks allBookMars = oDoc.Bookmarks;
- for (int i = 1; i <= allBookMars.Count; i++)
- {
- object oIndex = i;
- Bookmark aBookMark = allBookMars.get_Item(ref oIndex);
- if (aBookMark == null)
- {
- continue;
- }
- String sBookName = aBookMark.Name;
- if (CMyString.isEmpty(sBookName)) {
- continue;
- }
-
-
- if (!mappingProperties.contains(sBookName,true)) {
- continue;
- }
-
-
- oBookIdToRangeMap.Add(sBookName.Trim().ToUpper(), aBookMark.Range);
- }
-
-
- return oBookIdToRangeMap;
- }
-
-
-
-
-
-
-
- private void replaceMappingContent(_Document oDoc,Hashtable oBookIdToRangeMap, Properties mappingProperties)
- {
-
- if (oDoc == null || (oBookIdToRangeMap == null) || (mappingProperties == null)) {
- return;
- }
-
-
- foreach (String sBookIdKey in oBookIdToRangeMap.Keys)
- {
- Range currBookRange = (Range)oBookIdToRangeMap[sBookIdKey];
- if (currBookRange == null) {
- continue;
- }
-
-
- String sReplaceValue = mappingProperties.getPropertyAsString(sBookIdKey,true);
- sReplaceValue = CMyString.showEmpty(sReplaceValue);
- currBookRange.Text = sReplaceValue;
-
-
- if (!oDoc.Bookmarks.Exists(sBookIdKey))
- {
- object oTempBookRange = currBookRange;
- oDoc.Bookmarks.Add(sBookIdKey, ref oTempBookRange);
- }
- }
- }
-
-
- }
- }
C# 操作 Word 修改word的高级属性中的自定义属性
原文:http://www.cnblogs.com/Alex80/p/4349466.html