[C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)

来源:互联网 发布:联通软件研究院待遇 编辑:程序博客网 时间:2024/05/17 22:59

 

private Microsoft.Office.Interop.Word._Document m_Document = null; 

private Microsoft.Office.Interop.Word._Application m_wordApplication = null; 

private object oMissing = Type.Missing; 

 

//===================================================================== 

#region 開啟檔案 

 

/// <summary> 

/// 開啟檔案 

/// </summary> 

/// <param name="fileName">檔案</param> 

/// <param name="excuteInBackGround">前景執行</param> 

public void OpenWordDocument(object fileName, bool excuteInBackGround) 

   this.m_wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); 

 

   //不顯示警告或彈跳視窗。如果出現彈跳視窗,將選擇預設值繼續執行。 

   this.m_wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; 

   this.m_wordApplication.Visible = excuteInBackGround;      //前景或背景執行    

 

   //開啟檔案 

   this.m_Document = this.m_wordApplication.Documents.Open(ref fileName, 

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

#endregion 

 

//===================================================================== 

#region 另存新檔 

 

/// <summary> 

/// 另存新檔 

/// </summary> 

/// <param name="strFileName">檔名</param> 

public void SaveAsWordDocument(object strFileName) 

   //另存新檔 

   this.m_Document.SaveAs(ref strFileName, ref oMissing, ref oMissing, 

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 

            ref oMissing, ref oMissing, ref oMissing); 

#endregion 

 

//===================================================================== 

#region 儲存檔案 

 

/// <summary> 

/// 儲存檔案 

/// </summary> 

public void SaveWordDocument() 

   //另存新檔 

   this.m_Document.Save(); 

#endregion 

 

//===================================================================== 

#region 關閉檔案 

 

/// <summary> 

/// 關閉檔案 

/// </summary> 

public void CloseWordDocument() 

   object oSaveChanges = WdSaveOptions.wdSaveChanges; 

   this.m_Document.Close(ref oSaveChanges, ref oMissing, ref oMissing); 

   this.m_wordApplication.Quit(ref oSaveChanges, ref oMissing, ref oMissing); 

#endregion 

 

//===================================================================== 

#region 尋找並取代word內字串 

 

/// <summary> 

/// 尋找並取代字串 

/// </summary> 

/// <param name="findText">尋找的字串</param> 

/// <param name="replaceText">取代的字串</param> 

public void Replace(string findText, string replaceText) 

   object oStory = WdUnits.wdStory; 

   object oMove = WdMovementType.wdMove; 

   this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove); 

   Find find = this.m_wordApplication.Selection.Find; 

   find.ClearFormatting(); 

   find.Replacement.ClearFormatting(); 

   find.Text = findText; 

   find.Replacement.Text = replaceText; 

   this.ExecuteReplace(find); 

#endregion 

 

//===================================================================== 

#region 尋找並取代word內字串(字數大於255)或有斷行符號 

 

/// <summary> 

/// 尋找並取代word內字串(字數大於255)或有斷行符號 

/// </summary> 

/// <param name="findText">尋找的字串</param> 

/// <param name="replaceText">取代的字串</param> 

public void Replace255(string findText, string replaceText) 

   this.Search(findText); 

   this.m_wordApplication.Selection.TypeText(replaceText); 

#endregion 

 

//===================================================================== 

#region 尋找word內字串 

 

/// <summary> 

/// 尋找word內字串 

/// </summary> 

/// <param name="findText">尋找的字串</param> 

public bool Search(string findText) 

   object oStory = WdUnits.wdStory; 

   object oMove = WdMovementType.wdMove; 

   this.m_wordApplication.Selection.HomeKey(ref oStory, ref oMove); 

   Find find = this.m_wordApplication.Selection.Find; 

   find.ClearFormatting(); 

   find.Text = findText; 

   find.Replacement.ClearFormatting(); 

   return this.ExecuteReplace(find, oMissing); 

#endregion 

 

//===================================================================== 

//===================================================================== 

#region 執行取代字串 

 

/// <summary> 

/// 執行取代字串 

/// </summary> 

/// <param name="find">搜尋參數</param> 

/// <returns>是否成功</returns> 

private bool ExecuteReplace(Find find) 

   return this.ExecuteReplace(find, WdReplace.wdReplaceAll); 

#endregion 

 

//===================================================================== 

#region 實際執行取代字串方法 

 

/// <summary> 

/// </summary> 

/// <param name="find">搜尋參數</param> 

/// <param name="replaceOption">是否取代</param> 

/// <returns>是否成功</returns> 

private bool ExecuteReplace(Find find, object objReplaceOption) 

   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); 

#endregion 

 

 

转载自:http://itgroup.blueshop.com.tw/uuuiii00/AllenJ?n=convew&i=20320

原创粉丝点击