日期:2014-05-17  浏览次数:20436 次

C#操作Word书签 打印 本地测试OK 发布到IIS后打印无反应
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationClass();
  string TemplateFile = serverPath + "table\\fgfdgfdgfdg.doc";
  //生成的具有模板样式的新文件
  string docName = Guid.NewGuid().ToString();
  string FileName = serverPath + "table\\" + docName + ".doc";

  //模板文件拷贝到新文件
  if (File.Exists(FileName))
  File.Delete(FileName);
  File.Copy(TemplateFile, FileName);
  Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

  object Obj_FileName = FileName;
  object Visible = true;
  object ReadOnly = false;
  object missing = System.Reflection.Missing.Value;
  //打开文件
  doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
  ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref Visible,
  ref missing, ref missing, ref missing,
  ref missing);
  //doc.ActiveWindow.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal;
  doc.Activate();

  if (!projectID.Equals(string.Empty))
  {
  string sql = "select * from ProjectApproval_CheckMiddleDetail where ProjectID = '" + HiddenFieldID.Value + "' and CheckID = '" + HiddenFieldCheckID.Value + "'";
  DataSet dataset1 = DbHelperSQL.Query(sql);

  # region addInfo
  foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
  {
  if (bm.Name == "ProjectNo")
  {
  bm.Select();
  bm.Range.Text = dataset1.Tables[0].Rows[0]["ProjectNo"].ToString();
  }
  else if (bm.Name == "StartAndEndTime")
  {
  bm.Select();
  bm.Range.Text = dataset1.Tables[0].Rows[0]["StartAndEndTime"].ToString();
  }
  }
  }
  #endregion

  object IsSave = true;

  if (print)
  {
  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);
  }

  doc.Close(ref IsSave, ref missing, ref missing);
  app.Quit(ref IsSave, ref missing, ref missing);

  //app.Quit();
  if (!print)
  {
  Response.Clear();
  Response.ContentType = "application/msword";

  Response.AddHeader("Content-Disposition", docName);
  Response.Flush();
  Response.WriteFile(FileName);
  Response.End();
  }

  File.Delete(FileName);



点打印后执行到doc.PrintOut(ref missing, ref missing, ref missing, ref missing,