关闭MS Office的C#控制台 [英] Close MS Office C# Console

查看:261
本文介绍了关闭MS Office的C#控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个自动化测试,以确定是否RTF文件由微软Word成功打开。到目前为止,我经历了所有的RTFS循环给定的目录中,并打开它们。后来,我将不得不捕获异常来生成报告(日志坠毁字的文件名)。

我处理大量的文件。我的应用程序正在打开Word的一个新实例为每个文件。谁能告诉我如何关闭Word?

 公共类LoadRTFDoc
{
    私人目标文件名;
    私有对象只读;
    私有对象可见性;
    私人物件丢失;
    私人ApplicationClass WordApp;
    私有对象保存;
    私有对象OrigFormat;
    私有对象RouteDoc;    公共LoadRTFDoc(目标文件名)
    {
        this.WordApp =新ApplicationClass();
        this.FileName =文件名;
        只读= FALSE;
        ISVISIBLE = TRUE;
        缺少= System.Reflection.Missing.Value;
        保存= System.Reflection.Missing.Value;
        OrigFormat = System.Reflection.Missing.Value;
        RouteDoc = System.Reflection.Missing.Value;    }
    公共无效OpenDocument格式()
    {
        WordApp.Visible = TRUE;
        WordApp.Documents.Open(REF文件名,REF失踪,裁判只读,参考失踪,失踪参考,
                                   裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,
                                   REF可见性,REF失踪,失踪参考,参考失踪,失踪参考);
        WordApp.Activate();
    }
    公共无效CloseDocument()
    {
        WordApp.Documents.Close(REF保存,裁判OrigFormat,裁判RouteDoc);
    }}

每个文档打开后,我执行CloseDocument()方法。任何人有一些见解,我在这?

谢谢!


解决方案

  WordApp.Quit()

将退出应用程序。

然而,最安全的方式是得到一个处理过程,并杀死WINWORD过程。在C#以下code会做的是:

 的foreach(在Process.GetProcessesByName进程p(WINWORD))
{
    如果(!p.HasExited)
    {
        p.Kill();
    }
}

原因是,它会经常出现(我假设,特别是因为你正在测试不是Word中创建的文档),Word将一个开放的消息框挂起,例如修复对话框。在这种情况下杀死进程是关闭应用程序的最简单方式。

我会建议您先尝试使用 Application.Quit 来关闭Word。如果这不工作,它表示你的输入文件有问题(最有可能是因为维修对话框阻止字)。您应该将此记录为在日志中的错误,然后进行查杀WINWORD过程。

您可能面临的另一个问题是Word的文档恢复功能阻塞应用程序启动时(因此preventing被打开,直到客场点击恢复对话框中的文件)。文档恢复可以通过在这两个HKCU和HKLM删除以下注册表项启动Word之前被禁用(11.0与Word 2003和10.0的Word XP替换12.0):

 软件\\微软\\办公室\\ 12.0 \\字\\弹性

这不用说,杀字是一个相当粗鲁的方法,但是,它是简单和相当稳健的。在code以上会杀了字的任何实例的用户。如果你想杀死只是一个特定实例事情变得更加困难。你将不得不检索特定的Word实例的进程ID。通常,这可以通过搜索实例的窗口标题进行,例如使用WinAPI的功能,如 FindWindowByCaption GetWindowThreadProcessId

I'm writing an automated test to determine whether or not rtf files are successfully opened by MS Word. So far I looping through all the rtfs within a given directory and opening them. Later I will have to catch exceptions to generate a report (log the file name that crashed word).

I am processing a large number of files. My application is currently opening a new instance of Word for each file. Can someone tell me how to close Word?

public class LoadRTFDoc
{
    private object FileName;
    private object ReadOnly;
    private object isVisible;
    private object Missing;
    private ApplicationClass WordApp;
    private object Save;
    private object OrigFormat;
    private object RouteDoc;

    public LoadRTFDoc(object filename)
    {
        this.WordApp = new ApplicationClass();
        this.FileName = filename;
        ReadOnly = false;
        isVisible = true;
        Missing = System.Reflection.Missing.Value;
        Save = System.Reflection.Missing.Value;
        OrigFormat = System.Reflection.Missing.Value;
        RouteDoc = System.Reflection.Missing.Value;

    }


    public void OpenDocument()
    {
        WordApp.Visible = true;
        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 Missing, ref Missing, ref Missing, ref Missing);
        WordApp.Activate();
    }
    public void CloseDocument()
    {
        WordApp.Documents.Close(ref Save, ref OrigFormat, ref RouteDoc);
    }

}

I am executing the CloseDocument() method after each document is opened. Anyone have some insight for me on this?

Thanks!

解决方案

WordApp.Quit()

will exit the application.

However, the safest way is to get a handle to the process and kill the winword process. In C# the following code would do that:

foreach (Process p in Process.GetProcessesByName("winword"))
{
    if (!p.HasExited)
    {
        p.Kill();
    }
}

The reason is that it will happen frequently (I assume, especially since you are testing documents created not by Word) that Word will hang with an open message box, e.g. a repair dialog. In that case killing the process is the easiest way to close the application.

I would suggest that you first try to close Word using Application.Quit. If this does not work it indicates a problem with your input file (most likely because a repair dialog is blocking Word). You should record this as an error in your log and then proceed killing the winword process.

Another problem you might face is Word's document recovery feature blocking the application on startup (and thus preventing a document from being opened until the recovery dialog box is clicked away). Document recovery can be disabled by deleting the following registry key under both HKCU and HKLM prior to starting Word (replace 12.0 with 11.0 for Word 2003 and 10.0 for Word XP):

Software\Microsoft\Office\12.0\Word\Resiliency

It goes without saying that killing Word is a rather rude approach, however, it is simple and rather robust. The code above will just kill any instance of Word for a user. If you want to kill only a specific instance things get more difficult. You would have to retrieve the process id of a specific Word instance. Typically this can be done by searching for the window title of the instance, e.g. using WinAPI functions like FindWindowByCaption and GetWindowThreadProcessId.

这篇关于关闭MS Office的C#控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆