摘要:[.NET C# - Word] 利用陣列搜尋跟取代字串
001 using Microsoft.Office.Interop.Word;
002
003 public class WordHelper
004 {
005 private _Document m_Document = null;
006 private _Application m_wordApplication = null;
007
008 /// <summary>
009 /// 尋找取代執行迴圈
010 /// </summary>
011 /// <param name="tagValueList">表示相關 String 索引鍵和 String 值的集合,而這些可以利用索引鍵或索引來存取。 </param>
012 /// <param name="objWdSeekView">WdSeekView 的參數</param>
013 private void ReplaceTag(List<List2StringStructure> tagValueList, WdSeekView objWdSeekView)
014 {
015 // 指定搜尋的位置
016 this.m_Document.ActiveWindow.ActivePane.View.SeekView = objWdSeekView;
017 // 初始化參數
018 string strFind = string.Empty;
019 string strReplace = string.Empty;
020 if (tagValueList != null)
021 {
022 for (int i = 0; i < tagValueList.Count; i++)
023 {
024 strFind = tagValueList[i].strFind.ToString().Trim();
025 strReplace = tagValueList[i].strReplace.ToString().Trim();
026
027 if ((tagValueList[i].strReplace.ToString().Trim().Length < 255) || (tagValueList[i].strReplace.ToString().Trim().Split('\r').Length == 0))
028 {
029 this.Replace(strFind, strReplace);
030 }
031 else
032 {
033 this.Replace255(strFind, strReplace);
034 }
035 }
036 }
037 // 返回預設
038 this.m_Document.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
039 }
040
041 /// <summary>
042 /// 尋找並取代word內字串
043 /// </summary>
044 /// <param name="findText">代入值</param>
045 /// <param name="replaceText">取代值</param>
046 private void Replace(string findText, string replaceText)
047 {
048 object oStory = WdUnits.wdStory;
049 object oMove = WdMovementType.wdMove;
050 this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove);
051 Find find = this.m_wordApplication.Selection.Find;
052 find.ClearFormatting();
053 find.Replacement.ClearFormatting();
054 find.Text = findText;
055 find.Replacement.Text = replaceText;
056 this.ExecuteReplace(find);
057 }
058
059 /// <summary>
060 /// 尋找並取代word內字串(字數大於255)或有斷行符號
061 /// </summary>
062 /// <param name="findText">代入值</param>
063 /// <param name="replaceText">取代值</param>
064 private void Replace255(string findText, string replaceText)
065 {
066 this.Search(findText);
067 this.m_wordApplication.Selection.TypeText(replaceText);
068 }
069
070 /// <summary>
071 /// 尋找word內字串
072 /// </summary>
073 /// <param name="findText">代入值</param>
074 private bool Search(string findText)
075 {
076 object oStory = WdUnits.wdStory;
077 object oMove = WdMovementType.wdMove;
078 this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove);
079 Find find = this.m_wordApplication.Selection.Find;
080 find.ClearFormatting();
081 find.Text = findText;
082 find.Replacement.ClearFormatting();
083 return this.ExecuteReplace(find, oMissing);
084 }
085
086 /// <summary>
087 /// 執行取代字串
088 /// </summary>
089 /// <param name="find">搜尋參數</param>
090 /// <returns>成功/失敗</returns>
091 private bool ExecuteReplace(Find find)
092 {
093 return this.ExecuteReplace(find, WdReplace.wdReplaceAll);
094 }
095
096 /// <summary>
097 /// 實際執行取代字串方法
098 /// </summary>
099 /// <param name="find">搜尋參數</param>
100 /// <param name="replaceOption">取代/不取代</param>
101 /// <returns>成功/失敗</returns>
102 private bool ExecuteReplace(Find find, object objReplaceOption)
103 {
104 return find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref objReplaceOption, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
105 }
106 }
002
003 public class WordHelper
004 {
005 private _Document m_Document = null;
006 private _Application m_wordApplication = null;
007
008 /// <summary>
009 /// 尋找取代執行迴圈
010 /// </summary>
011 /// <param name="tagValueList">表示相關 String 索引鍵和 String 值的集合,而這些可以利用索引鍵或索引來存取。 </param>
012 /// <param name="objWdSeekView">WdSeekView 的參數</param>
013 private void ReplaceTag(List<List2StringStructure> tagValueList, WdSeekView objWdSeekView)
014 {
015 // 指定搜尋的位置
016 this.m_Document.ActiveWindow.ActivePane.View.SeekView = objWdSeekView;
017 // 初始化參數
018 string strFind = string.Empty;
019 string strReplace = string.Empty;
020 if (tagValueList != null)
021 {
022 for (int i = 0; i < tagValueList.Count; i++)
023 {
024 strFind = tagValueList[i].strFind.ToString().Trim();
025 strReplace = tagValueList[i].strReplace.ToString().Trim();
026
027 if ((tagValueList[i].strReplace.ToString().Trim().Length < 255) || (tagValueList[i].strReplace.ToString().Trim().Split('\r').Length == 0))
028 {
029 this.Replace(strFind, strReplace);
030 }
031 else
032 {
033 this.Replace255(strFind, strReplace);
034 }
035 }
036 }
037 // 返回預設
038 this.m_Document.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
039 }
040
041 /// <summary>
042 /// 尋找並取代word內字串
043 /// </summary>
044 /// <param name="findText">代入值</param>
045 /// <param name="replaceText">取代值</param>
046 private void Replace(string findText, string replaceText)
047 {
048 object oStory = WdUnits.wdStory;
049 object oMove = WdMovementType.wdMove;
050 this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove);
051 Find find = this.m_wordApplication.Selection.Find;
052 find.ClearFormatting();
053 find.Replacement.ClearFormatting();
054 find.Text = findText;
055 find.Replacement.Text = replaceText;
056 this.ExecuteReplace(find);
057 }
058
059 /// <summary>
060 /// 尋找並取代word內字串(字數大於255)或有斷行符號
061 /// </summary>
062 /// <param name="findText">代入值</param>
063 /// <param name="replaceText">取代值</param>
064 private void Replace255(string findText, string replaceText)
065 {
066 this.Search(findText);
067 this.m_wordApplication.Selection.TypeText(replaceText);
068 }
069
070 /// <summary>
071 /// 尋找word內字串
072 /// </summary>
073 /// <param name="findText">代入值</param>
074 private bool Search(string findText)
075 {
076 object oStory = WdUnits.wdStory;
077 object oMove = WdMovementType.wdMove;
078 this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove);
079 Find find = this.m_wordApplication.Selection.Find;
080 find.ClearFormatting();
081 find.Text = findText;
082 find.Replacement.ClearFormatting();
083 return this.ExecuteReplace(find, oMissing);
084 }
085
086 /// <summary>
087 /// 執行取代字串
088 /// </summary>
089 /// <param name="find">搜尋參數</param>
090 /// <returns>成功/失敗</returns>
091 private bool ExecuteReplace(Find find)
092 {
093 return this.ExecuteReplace(find, WdReplace.wdReplaceAll);
094 }
095
096 /// <summary>
097 /// 實際執行取代字串方法
098 /// </summary>
099 /// <param name="find">搜尋參數</param>
100 /// <param name="replaceOption">取代/不取代</param>
101 /// <returns>成功/失敗</returns>
102 private bool ExecuteReplace(Find find, object objReplaceOption)
103 {
104 return find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref objReplaceOption, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
105 }
106 }