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

Quitメソッドは、(ちょうどWINWORDものでExcelオブジェクトを置き換える)これをチェックする助けにはなりません場合は、次のhttp://blogs.msdn.com/b/msdnforum/archive/2010/03/09/excel-does-not-quit-after-automation-from-net-side.aspx

EDIT: とハードコアソリューション:

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(); 
    } 
} 
関連する問題