如何关闭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; } }
我得到了很多文档对象的方法,但只有一个.Close()
关闭,其中有这样的参数: ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument
,我不明白。
什么是理想的方式? 谢谢..
编辑:
-
我无法关闭整个Word应用程序(WinWord),因为用户可能打开了其他word文件。
-
我需要终止word实例(类似于
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, ie: 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
写入WINWORD
进程的ID,然后比较List
的值。 如果在List
找不到processID
则会使用此ID终止进程。
public List getRunningProcesses() { List ProcessIDs = new List (); //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 processesbeforegen = getRunningProcesses(); // APP CREATION/ DOCUMENT CREATION HERE... List processesaftergen = getRunningProcesses();
然后运行killProcesses(processesbeforegen, processesaftergen);
private void killProcesses(List processesbeforegen, List 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.Application app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); if (app == null) return true; app.Quit(false);
- 即使EnableRaisingEvents设置为true,也永远不会调用Process.Exited
- 将WPF Button CommandParameter绑定到DataTemplate中的Button本身
- Caliburn.Micro在App.xaml的命名空间中没有“Bootstrapper”
- 使用C#中的复杂用户定义类型(UDT)调用VB6 DLL函数
- 发送XML时,AspNet WebApi POST参数为null
- 如何检索嵌入的xml资源?
- 成功注册了COM DLL,但不能使用类方法
- 在后面的代码中访问ViewModel是否总是违反MVVM模式?
- 跟踪数据库的进度保存在“live”进度条asp.net mvc中