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

本文介绍如何使用VBScript进行Word文档的打开、保存、关闭等操作,并提供了多个实例来展示具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值