AmirHossein
AmirHossein

Reputation: 367

Closing a process that open in code

I create a WordTemplate with some placeholders for field,in code I insert value in this placeholders and show it to user.

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

it work good and successfully! but when a user close the Word,her Process not closed and exists in Task Manager Process list. this process name is WINWORD.exe I know that I can close process whit code [process.Kill()] but I don't know which process that I should to kill. if I want to kill all process with name [WINWORD.exe] all Word window closed.but I want to close specific Word window and kill process that I opened.

How to do it?

Upvotes: 3

Views: 2724

Answers (2)

cookieMonster
cookieMonster

Reputation: 597

If Quit method won't help check this out (just replace excel object with winword ones): http://blogs.msdn.com/b/msdnforum/archive/2010/03/09/excel-does-not-quit-after-automation-from-net-side.aspx

EDIT: and hardcore solution:

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

Upvotes: 2

Skonen
Skonen

Reputation: 631

Why not just use Microsoft.Office.Interop.Word.Application.Quit with the app you have opened (WordApp)?

I should note in doing so, you should of course hold on to your WordApp reference and not nullify it until after you have called Quit.

Upvotes: 0

Related Questions