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