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



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

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

这是我尝试的:

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())没有任何提示用户是否保存等

从这里得到的解决方案。

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;

不如这样写:

修改自: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;
}

我知道已经太晚了,但是我发现了这个主题,因为我有同样的问题。

我的解决方案可能会帮助到其他人。

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

所以这不是用户友好的

我的代码在c#中检查Word.Application对象创建之前/之后的进程,并在List<int>中写入WINWORD进程的id然后比较两个List<int>中的值。如果在两个List<int>中都没有找到processID,它会杀死这个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之前)。

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

如果你想关闭整个word应用程序,只需调用:

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

最新更新