[C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
2011-04-08 11:23
489 查看
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
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
相关文章推荐
- C#操作Word导致 “Microsoft.Office.Interop.Word.ApplicationClass”的 COM 组件错误的解决方法
- C#引用Office.word出错的解决办法-无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass” 【转】
- Microsoft.Office.Interop.Word 对word 的基本操作
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel) 收藏
- (转)无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此操作失败的原因是对 IID 为“{00020970-
- Microsoft.Office.Interop.Word 对word 的基本操作
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- .net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。
- Microsoft.Office.Interop.Word引用- 无法嵌入互操作类型
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- 解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”
- C#导出到Excel——无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
- Excel C# Excel 操作 [ 转载] Microsoft.Office.Interop.Excel 讀 / 寫 Excel 語法概全
- 对word进行操作, 提示不能加载程序集 Microsoft.Office.Interop.Word.dll