2012-04-12 21 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並殺死那個? – 2012-04-12 08:47:02

+2

我會嘗試'Marshal.ReleaseComObject' – 2012-04-12 09:03:23

+0

什麼是Marshal的名字空間? – AmirHossein 2012-04-12 13:31:30

回答

2

如果Quit方法將不利於檢查了這一點(只更換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(); 
    } 
} 
相關問題