相关疑难解决方法(0)

如何关闭Word文档的运行实例?(C#)

我可以看到很多非常相似的线程,但似乎没有什么能给我一个应该是非常基本的解决方案.

从我的winforms应用程序中,我需要关闭一个word文档的运行实例(从应用程序本身打开).当我从应用程序中打开word文档时,我会在列表中跟踪它.现在我该如何关闭同一个doc?

这是我尝试过的:

private bool CloseWord(string osPath) //here I pass the fully qualified path of the file
{
    try
    {
        Word.Application app = (Word.Application)Marshal.GetActiveObject("Word.Application");
        if (app == null)
            return true;

        foreach (Word.Document d in app.Documents)
        {
            if (d.FullName.ToLower() == osPath.ToLower())
            {
               d.What? //How to close here?
               return true;
            }
        }
        return true;
    }
    catch
    {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了很多文档对象的方法,但只有一个.Close()关闭哪个有这样的参数:ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument我不明白.

什么是理想的方式?谢谢..

编辑:

  1. 我无法关闭整个Word应用程序(WinWord),因为用户可能打开了其他word文件.

  2. 我需要终止单词实例(类似的东西Process.Kill())而不提示用户保存或不保存等.

c# office-automation running-other-programs c#-2.0

7
推荐指数
1
解决办法
2万
查看次数