c# - 关闭在代码中打开的进程

标签 c# ms-word ms-office word-template

我创建了一个带有字段占位符的 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 窗口并终止我打开的进程。

怎么做?

最佳答案

如果 Quit 方法无法帮助检查这一点(只需将 excel 对象替换为 winword 对象):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();
    }
}

关于c# - 关闭在代码中打开的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10120232/

相关文章:

时间:2019-05-17 标签:c# "as"vs "()"conversion

java - 从服务通过 JACOB 调用时,Office 2007 无法打开文件

c++ - 在 C++ 中读取 .docx

vb6 - VB6中Print的一个奇怪案例

c# - ASP.net Identity 自定义用户模型未被角色、声明和登录表使用

c# - 创建用户时出错 {"odata.error": {"code" :"Request_BadRequest"

.net - 使用.NET自动更新Word文档的目录部分?

ms-office - 从 Outlook JavaScript Api (office.js) 调用时,Exchange EWS 的 UpdateItem 中的访问被拒绝

c# - C# : What are technical reasons to prefer///or/** 中的文档注释

c# - String.Trim() 删除的内容超出需要?