/// <summary>
/// 轉換word檔案文字
/// </summary>
/// <param name="filename"></param>
private void ConvertTo(string filename)
{
//建立word應用程式對象
Microsoft.Office.Interop.Word.Application G_WordApplication =
new Microsoft.Office.Interop.Word.Application();
//建立object對象
object P_FilePath = filename;
//@"E:\合同\合同\房屋托管協定1.doc"
//object P_FilePath = System.Windows.Forms.Application.StartupPath.ToString() + "\\word.doc";
object G_Missing = System.Reflection.Missing.Value;
//打開word文檔
Microsoft.Office.Interop.Word.Document P_Document = G_WordApplication.Documents.Open(
ref P_FilePath, ref G_Missing, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing);
//獲得文檔範圍
Microsoft.Office.Interop.Word.Range P_Range =
P_Document.Range(ref G_Missing, ref G_Missing);
//得到find對象
Microsoft.Office.Interop.Word.Find P_Find = P_Range.Find;
this.Invoke(
(MethodInvoker)(() =>
{
if (isAudit)
{
string[] strs = this.cmbFile.Text.Split('-');
string write = users.Name;//需要填寫者
var items = (from i in cepg.CConvertDetail
from j in cepg.CConvert
where i.WriteUser == write && i.ConvertID == j.ID && j.FileName == this.cmbFile.Text && i.Status == 0
select new
{
i.Type,
i.Value,
}).ToList();
if (items.Count > 0)
{
foreach (var item in items)
{
P_Find.Text = item.Type; //設定查找的文本
P_Find.Replacement.Text = item.Value; //替換的文本
}
}
}
}));
//定義替換方式對象
object P_Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
//執行替換
bool P_bl = P_Find.Execute(ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing, ref G_Missing,
ref P_Replace, ref G_Missing, ref G_Missing,
ref G_Missing, ref G_Missing);
//儲存文檔
G_WordApplication.Documents.Save(ref G_Missing, ref G_Missing);
//關閉文檔
((Microsoft.Office.Interop.Word._Document)P_Document).Close(ref G_Missing, ref G_Missing, ref G_Missing);
//退出word應用程式
((Microsoft.Office.Interop.Word._Application)G_WordApplication).Quit(ref G_Missing, ref G_Missing, ref G_Missing);
}