2012-04-12 7 views
2

Ich erstelle ein WordTemplate mit einigen Platzhaltern für Feld, in Code füge ich Wert in diesen Platzhaltern ein und zeige es dem Benutzer an.Einen Prozess schließen, der im Code geöffnet wird

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!"); 
     } 

es funktioniert gut und erfolgreich! Wenn jedoch ein Benutzer das Word schließt, wird ihr Prozess nicht geschlossen und ist in der Task-Manager-Prozessliste enthalten. dieser Prozessname ist WINWORD.exe Ich weiß, dass ich Prozess mit Code [process.Kill()] schließen kann, aber ich weiß nicht, welchen Prozess ich töten sollte. wenn ich alle Prozesse mit dem Namen [WINWORD.exe] alle Word-Fenster geschlossen werden soll.aber ich möchte bestimmte Word-Fenster schließen und den Prozess, den ich geöffnet habe.

Wie geht das?

+0

Ich denke, die beste Lösung für Sie ist die ID des Threads zu finden und diese zu töten? –

+2

Ich würde versuchen, Marshal.ReleaseComObject' –

+0

Was ist Marschall Namespace? – AmirHossein

Antwort

0

Warum nicht einfach Microsoft.Office.Interop.Word.Application.Quit mit der App verwenden, die Sie geöffnet haben (WordApp)?

Ich sollte dabei beachten, sollten Sie natürlich halten Sie auf Ihre WordApp-Referenz und nicht annullieren, bis Sie Quit aufgerufen haben.

2

Wenn Quit-Methode wird dies nicht helfen, check out (ersetzen Sie einfach Excel-Objekt mit winword sind): http://blogs.msdn.com/b/msdnforum/archive/2010/03/09/excel-does-not-quit-after-automation-from-net-side.aspx

EDIT: und Hardcore-Lösung:

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(); 
    } 
} 
Verwandte Themen