2012-04-12 9 views
2

필드에 대한 일부 자리 표시자가있는 WordTemplate을 만듭니다. 코드에서이 자리 표시 자에 값을 삽입하고이를 사용자에게 보여줍니다.코드에서 열려있는 프로세스를 닫습니다.

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string DocFilePath = ""; 
     //string FilePath = System.Windows.Forms.Application.StartupPath; 
     object fileName = @"[...]\asset\word templates\FormatPeygiri1.dot"; 
     DocFilePath = fileName.ToString(); 

     FileInfo fi = new FileInfo(DocFilePath); 
     if (fi.Exists) 
     { 
      object readOnly = false; 
      object isVisible = true; 

      object PaperNO = "PaperNO"; 
      object PaperDate = "PaperDate"; 
      object Peyvast = "Peyvast"; 

      object To = "To"; 
      object ShoName = "ShoName"; 
      object DateName = "DateName"; 

      Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, 
       ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
       ref isVisible, ref isVisible, ref missing, ref missing, ref missing); 

      WordApp.ActiveDocument.FormFields.get_Item(ref PaperNO).Result = TextBox_PaperNO.Text; 

      string strPaperDate = string.Format("{0}/{1}/{2}", PersianDateTimeHelper.GetPersainDay(DateTimePicker_PaperDate.SelectedDate), 
               PersianDateTimeHelper.GetPersainMonth(DateTimePicker_PaperDate.SelectedDate), 
               PersianDateTimeHelper.GetPersainYear(DateTimePicker_PaperDate.SelectedDate)); 

      WordApp.ActiveDocument.FormFields.get_Item(ref PaperDate).Result = strPaperDate; 

      WordApp.ActiveDocument.FormFields.get_Item(ref Peyvast).Result = TextBox_Peyvast.Text; 

      WordApp.ActiveDocument.FormFields.get_Item(ref To).Result = TextBox_To.Text; ; 
      WordApp.ActiveDocument.FormFields.get_Item(ref ShoName).Result = TextBox_ShoName.Text; 

      string strDateName = string.Format("{0}/{1}/{2}", PersianDateTimeHelper.GetPersainDay(DateTimePicker_DateName.SelectedDate), 
               PersianDateTimeHelper.GetPersainMonth(DateTimePicker_DateName.SelectedDate), 
               PersianDateTimeHelper.GetPersainYear(DateTimePicker_DateName.SelectedDate)); 

      WordApp.ActiveDocument.FormFields.get_Item(ref DateName).Result = strDateName; 

      aDoc.Activate(); 
      WordApp.Visible = true; 
      aDoc = null; 
      WordApp = null; 
     } 
     else 
     { 
      MessageBox1.Show("File Not Exist!"); 
     } 

잘 작동합니다. 하지만 사용자가 Word를 닫으면 프로세스가 닫히지 않고 작업 관리자 프로세스 목록에 있습니다. 이 프로세스 이름은 WINWORD.exe 입니다. 프로세스 오순절 코드 [process.Kill()]을 닫을 수 있지만 어떤 프로세스를 죽여야하는지 알 수 없습니다. 이름이 [WINWORD.exe] 인 모든 프로세스를 강제 종료하려면 모든 Word 창이 닫힙니다. 그러나 특정 Word 창을 닫고 내가 열었던 프로세스를 종료하고 싶습니다.

어떻게 만드시겠습니까?

+0

내가 당신을 위해 가장 좋은 방법이라고 생각을 스레드의 ID를 찾아서 죽여야합니까? –

+2

'Marshal.ReleaseComObject' 시도해 보겠습니다. –

+0

마샬의 네임 스페이스는 무엇입니까? – AmirHossein

답변

2

종료 방법 (단지 WINWORD 사람과 Excel 개체 대체)이 체크 아웃 도움이되지 않는 경우 : http://blogs.msdn.com/b/msdnforum/archive/2010/03/09/excel-does-not-quit-after-automation-from-net-side.aspx

이 편집 : 하드 코어 솔루션 :

public static class WinWordKiller 
{ 
     [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, 
CharSet = CharSet.Unicode, ExactSpelling = true, 
CallingConvention = CallingConvention.StdCall)] 
    private static extern long GetWindowThreadProcessId(long hWnd, out long lpdwProcessId); 

    public static void Kill(ref Microsoft.Office.Interop.Word.Application app) 
    { 
     long processId = 0; 
     long appHwnd = (long)app.Hwnd; 

     GetWindowThreadProcessId(appHwnd, out processId); 

     Process prc = Process.GetProcessById((int)processId); 
     prc.Kill(); 
    } 
}