public static Word._Application oWord = null;
public static Word._Document oDoc = null;
public static object oMissing = System.Reflection.Missing.Value;
/ <summary>
/ 实现套打功能
/ </summary>
private void Print(string cmbCODE, string dtpSIGN_DATE, string txtBAN_NAME, string txtBEIYONG4, string txtPRO_USER, string rtbWTMS, string txtPLACE, string txtTIME_LIMINT, string txtLIST, string lbFUYI_PRO, string lbSUSONG_PRO,string TemplatePath,string ResultPath,bool isShowForm)
{
// oMissing = System.Reflection.Missing.Value;
//创建一个Word应用程序实例oWord = new Word.Application();
//设置为不可见
//oWord.Visible = false;
//模板文件地址,这里假设在E盘根目录
// object oTemplate = "E:\\我的文档\\Print套打\\现场处罚决定书1.doc";
object oTemplate = TemplatePath.ToString();
//以模板为基础生成文档
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
//声明书签数组
object[] oBookMark = new object[17];
//赋值书签名
oBookMark[0] = "text1";
oBookMark[1] = "text2";
oBookMark[2] = "text3";
oBookMark[3] = "text4";
oBookMark[4] = "text5";
oBookMark[5] = "text6";
oBookMark[6] = "text7";
oBookMark[7] = "text8";
oBookMark[8] = "text9";
oBookMark[9] = "text10";
oBookMark[10] = "text11";
oBookMark[11] = "text12";
oBookMark[12] = "text13";
oBookMark[13] = "text14";
oBookMark[14] = "text15";
oBookMark[15] = "text16";
string strZhi = cmbCODE.Substring(cmbCODE.IndexOf('[') + 1, cmbCODE.IndexOf(']') - cmbCODE.IndexOf('[') - 1);
string strHao = cmbCODE.Substring(cmbCODE.IndexOf("第") + 1, cmbCODE.IndexOf("号") - cmbCODE.IndexOf("第") - 1);
oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "沪";
oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = strZhi;
oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = strHao;
oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = txtBAN_NAME; //物品持有人姓名/行政相对人
oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = txtBEIYONG4; //地址
oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = txtPRO_USER; //产品名称/生产过程
oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = rtbWTMS; //涉嫌(存在)问题
oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = txtPLACE; //登记保存地点
oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = txtTIME_LIMINT; //登记保存期限
oDoc.Bookmarks.get_Item(ref oBookMark[9]).Range.Text = txtLIST; //涉案物品清单
string[] FUYI_PRO = lbFUYI_PRO.Split('、');
oDoc.Bookmarks.get_Item(ref oBookMark[10]).Range.Text = FUYI_PRO[0].ToString(); //复议机关
oDoc.Bookmarks.get_Item(ref oBookMark[11]).Range.Text = FUYI_PRO[1].ToString(); //诉讼机关
oDoc.Bookmarks.get_Item(ref oBookMark[12]).Range.Text = lbSUSONG_PRO; //诉讼机关
string[] strChar = dtpSIGN_DATE.Split('-');
oDoc.Bookmarks.get_Item(ref oBookMark[13]).Range.Text = strChar[0]; //签发日期
oDoc.Bookmarks.get_Item(ref oBookMark[14]).Range.Text = strChar[1];
oDoc.Bookmarks.get_Item(ref oBookMark[15]).Range.Text = strChar[2];
//弹出保存文件对话框,保存生成的Words
try
{
#region 弹框保存
//SaveFileDialog sfd = new SaveFileDialog();
//sfd.Filter = "Word Document(*.doc)|*.doc";
//sfd.DefaultExt = "Word Document(*.doc)|*.doc";
//if (sfd.ShowDialog() == DialogResult.OK)
//{
// object filename = sfd.FileName;
// oDoc.SaveAs(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);
// oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
// //关闭word
// oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
//}
#endregion
object filename = ResultPath.ToString();
oWord.Visible = false;
oDoc.SaveAs(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);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭word
//oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
if (isShowForm == true)
{
WritManage.FrmWritPrint frmPrint = new Demo.WritManage.FrmWritPrint();
frmPrint.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
/// <summary>
/// 套打印当前文档方法
/// </summary>
/// <param name="strResultFilePath">套打的文件路径</param>
public static void printOut(string strResultFilePath)
{
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object templateFile = strResultFilePath.ToString();
try
{
app = new Microsoft.Office.Interop.Word.ApplicationClass();
doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
//打印
doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
//销毁word进程
finally
{
object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
if (doc != null)
doc.Close(ref saveChange, ref missing, ref missing);
if (app != null)
app.Quit(ref missing, ref missing, ref missing);
}
}