2013-02-25 3 views
1

필자는 응용 프로그램에서 인쇄를 쉽게하기 위해 함수를 사용합니다. 여기에 있습니다 :내 코드가 두 개의 Microsoft Office Word 인스턴스를 여는 이유는 무엇입니까?

public void print(string fileName, string variables, string values, string template) 
     { 
      // CREATE TEXT FILE FOR PRINTING 


     FileInfo fi = new FileInfo(fileName); 
     Object oMissing = System.Reflection.Missing.Value; 
     Object oTrue = true; 
     Object oFalse = false; 

     Word.Application _wordApp = new Word.Application(); 
     Word.Document oDoc = _wordApp.Documents.Add(template); 

     try 
     { 
      // Check if file already exists. If yes, delete it. 
      if (fi.Exists) 
      { 
       fi.Delete(); 
      } 

      // Create a new file 
      using (StreamWriter sw = fi.CreateText()) 
      { 
       sw.WriteLine(variables); 
       sw.WriteLine(values); 
      } 


      _wordApp.Visible = true; 
      oDoc.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters; 
      oDoc.MailMerge.OpenDataSource(fileName, false, false, true); 

      oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument; 
      oDoc.MailMerge.Execute(false); 
      oDoc.MailMerge.Application.PrintOut(); 
     } 
     catch (Exception Ex) 
     { 
      MessageBox.Show(Ex.ToString()); 
     } 
     finally 
     { 
      //CLEAR RESOURCES 
      ((Word._Document)oDoc).Close(ref oFalse, ref oMissing, ref oMissing); 
      ((Word._Application)_wordApp).Quit(); 
     } 

    } 

편지 병합을 사용하고 작업을 더 쉽게 할 수 있으므로 잘 보입니다. 사용자가 편지 병합을 위해 사용할 template 파일을 선택할 수 있도록 열린 파일 대화 상자를 엽니 다. 그러나, 응용 프로그램이 채워 모든 Merge Fields와 다른 문서를 열어 편지 병합을 Merge Fields의 모든 데이터없이 template 파일을 열고 다음 수행한다.

이런 일이 발생하는 이유 어떤 아이디어? 당신이 미리 문서를 미리 볼 것 파일 열기 윈도우 동안 다음 활성화하고 새로운 WINWORD 프로세스를 시작 경우 윈도우 7에서

   //CHOOSE TEMPLATE FILE 
       OpenFileDialog ofd = new OpenFileDialog(); 
       ofd.Filter = "Microsoft Word Template (*.dotx)|*.dotx"; 
       string filter = ofd.Filter; 
       ofd.Multiselect = false; 
       ofd.Title = "Open Template File"; 
       if (ofd.ShowDialog() == DialogResult.OK) 
       { 
        if (ofd.SafeFileName == "payslip.dotx") 
        { 
         //RETREIVE VALUES 
         var db = new DBConnect(); 
         string[] values = new string[20]; 
         bool print = false; 
         OleDbCommand cmd = null; 
         OleDbDataReader dr = null; 
         try 
         { 
          if (db.OpenConnection() == true) 
          { 
           string query = "SELECT * FROM employee WHERE employee_ID = " + idTxtBox.Text + ""; 
           cmd = new OleDbCommand(query, db.mycon); 
           dr = cmd.ExecuteReader(); 
           while (dr.Read()) 
           { 
            values[2] = (dr["employeeName"].ToString()); 
            values[3] = (dr["withTax"].ToString()); 
            values[4] = (dr["sss"].ToString()); 
            values[5] = (dr["pagIbig"].ToString()); 
            values[6] = (dr["pHealthGov"].ToString()); 
            values[7] = (dr["pCareOff"].ToString()); 
            values[8] = (dr["loan_sss"].ToString()); 
            values[9] = (dr["loan_pagIbig"].ToString()); 
            values[10] = (dr["loan_koti"].ToString()); 
            values[11] = (dr["tardinessAbscences"].ToString()); 
            values[12] = totalDeductionsTxtBox.Text; 
            values[13] = (dr["overTime"].ToString()); 
            values[14] = (dr["leave"].ToString()); 
           } 
           print = true; 
          } 
         } 
         catch (OleDbException ex) 
         { 
          MessageBox.Show(ex.Message); 
         } 
         finally 
         { 
          if (dr.IsClosed == false) 
          { 
           dr.Close(); 
          } 
          db.CloseConnnection(); 
         } 

         if (print == true) 
         { 
          // PRINTING 
          var p = new printClass(); 
          p.print(@"C:\IT Box Incorporated\Payroll\payslipCSV.csv", 
            "date_issued,employee_name,tax,sss,pagibig,phg,pco,sssloan,pagibigloan,kotiloan,late,total,ot,leave", 
            "" + (DateTime.Now.Date.ToString("MMM") + " " + DateTime.Now.Date.ToString("yyyy")) + "," + values[2] + "," + values[3] + "," + values[4] + "," + values[5] + "," + values[6] + "," + values[7] + "," + values[8] + "," + values[9] + "," + values[10] + "," + values[11] + "," + values[12] + "," + values[13] + "," + values[14] + "", 
            ofd.FileName); 
         } 
         else 
         { 
          MessageBox.Show("Print Failed!"); 
         } 
        } 
        else 
        { 
         MessageBox.Show("Print Failed! Wrong File!"); 
        } 
       } 
       else 
       { 
        MessageBox.Show("Print Failed!"); 
       } 

답변

1

다음은 파일 열기 대화 상자에 대한 코드입니다.

윈도우 7에서 미리보기 모드를 사용하지 않도록 설정