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

时间:2011-09-16 13:58:53

标签: c# c#-2.0 office-automation running-other-programs

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

从我的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;
    }
}

我为文档对象提供了很多方法,但只有一个.Close()关闭,其中包含这样的参数:ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument我不明白。

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

编辑:

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

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

4 个答案:

答案 0 :(得分:14)

此解决方案来自here解决方案。

Word.Application app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if (app == null)
    return true;

foreach (Word.Document d in app.Documents)
{
    if (d.FullName.ToLower() == osPath.ToLower())
    {
        object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
        object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
        object routeDocument = false;
        d.Close(ref saveOption, ref originalFormat, ref routeDocument);
        return true;
    }
}
return true;

答案 1 :(得分:2)

如何使用以下内容:

修改自:http://www.dreamincode.net/code/snippet1541.htm

public static bool KillProcess(string name)
{
    //here we're going to get a list of all running processes on
    //the computer
    foreach (Process clsProcess in Process.GetProcesses())
    {
        if (Process.GetCurrentProcess().Id == clsProcess.Id)
            continue;
        //now we're going to see if any of the running processes
        //match the currently running processes. Be sure to not
        //add the .exe to the name you provide, i.e: NOTEPAD,
        //not NOTEPAD.EXE or false is always returned even if
        //notepad is running.
        //Remember, if you have the process running more than once, 
        //say IE open 4 times the loop thr way it is now will close all 4,
        //if you want it to just close the first one it finds
        //then add a return; after the Kill
        if (clsProcess.ProcessName.Contains(name))
        {
            clsProcess.Kill();
            return true;
        }
    }
    //otherwise we return a false
    return false;
}

答案 2 :(得分:1)

我知道为时已晚,但我找到了这个话题,因为我遇到了同样的问题。

我的解决方案可以帮助其他人。

上面的解决方案不能很好地工作,因为它杀死了每个运行的Word实例,尽管它不是由c#程序打开而是用户打开。

所以这不是用户友好的。

我的代码在c#中创建Word.Application对象之前/之后检查进程,并在WINWORD中写入List<int>进程的ID 然后它比较List<int>中的值。 如果在processID中找不到List<int>,则会使用此ID终止该进程。

public List<int> getRunningProcesses()
{
    List<int> ProcessIDs = new List<int>();
    //here we're going to get a list of all running processes on
    //the computer
    foreach (Process clsProcess in Process.GetProcesses())
    {
        if (Process.GetCurrentProcess().Id == clsProcess.Id)
            continue;             
        if (clsProcess.ProcessName.Contains("WINWORD"))
        {
            ProcessIDs.Add(clsProcess.Id);
        }
    }
    return ProcessIDs;
}

运行两次(在创建Word.Application之前一次,之后运行一次)。

 List<int> processesbeforegen = getRunningProcesses();
 // APP CREATION/ DOCUMENT CREATION HERE...
 List<int> processesaftergen = getRunningProcesses();

然后运行killProcesses(processesbeforegen, processesaftergen);

 private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
 {
    foreach (int pidafter in processesaftergen)
    {
        bool processfound = false;
        foreach (int pidbefore in processesbeforegen)
        {
            if (pidafter == pidbefore)
            {
                processfound = true;
            }
        }

        if (processfound == false)
        {
            Process clsProcess = Process.GetProcessById(pidafter);
            clsProcess.Kill();
        }
    }
 }

答案 3 :(得分:1)

如果要关闭整个单词应用程序,可以调用:

Word.Application app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if (app == null)
    return true;
app.Quit(false);
相关问题