[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
- [C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
- WinForm利用Microsoft.Office.Interop.Word來合併多個Word檔
- Microsoft.Office.Interop.Word 对word 的基本操作
- Microsoft.Office.Interop.Word 对word 的基本操作
- Microsoft.Office.Interop.Word 创建word
- Microsoft.Office.Interop.Word 创建word
- Word转Html ( Microsoft.Office.Interop.Word )
- Microsoft.Office.Interop.Word 创建word
- Microsoft.Office.Interop.Word不规则表格操作注意事项
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”
- Microsoft.Office.Interop.Word引用- 无法嵌入互操作类型
- 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”
- 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函 无法嵌入互操作类型“Microsoft.Office.Interop.Word.App
- 关于Microsoft.Office.Interop.Word类库
- 系统找不到 Microsoft.Office.Interop.Word ---解决方案
- 解决引用Microsoft.Office.Core 和Microsoft.Office.Interop.Word
- 对word进行操作, 提示不能加载程序集 Microsoft.Office.Interop.Word.dll
- .net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作
- UART
- 4.2.2 计算数据
- asp.net 日期控件 My97DatePicker
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- 传智播客-ejb3(1)-理解应用服务器、分布式应用和ejb服务
- [C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
- eclipse调试以及step into step over step return区别
- ubuntu10.10的cvs安装教程总结
- 传智播客-ejb3(2)-bean类型、jboss基本应用、本地组件和远程组件及其底层实现原理简介
- 多路复用器
- 端口映射帮助文档
- 【转】8个高质量图标的最佳搜索引擎
- 常用短距离无线通信优缺点的纵横比较
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的区别与用法 - [Java|技术相关]