How to close an executable instance of a Word document? (FROM#)

I saw a lot of very similar flows around, but nothing seems to give me a solution, which should be very simple.

In my winforms application, I need to close the executable instance of a text document (open from the application itself). When I open a word document from the application, I track it in the list. Now, how can I close the same document?

Here is what I tried:

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

I get many methods for the document object, but only .Close() for closing, which has the following arguments: ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument , which I do not understand.

What is the perfect way? Thanks..

Edit:

  • I can not close the Word application (WinWord), because other text files can be opened by users.

  • I just need to end the instance of the word (something like Process.Kill() ) without any hints to save or not, etc.

+6
source share
4 answers

This solution, obtained from here , solves.

 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; 
+11
source

How about using something like:

Changed From: 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; } 
+2
source

I know that it is too late, but I found this topic because I had the same problem.

And my decision can help other people.

The solution above does not work well, because it killed every running instance of Word, although it was not opened by C # except the user.

So it was not friendly.

My code checks the processes before / after creating the Word.Application object in C # and writes the WINWORD process identifiers to the List<int> then it compares the values ​​as in the List<int> . When processID not found in both List<int> , it kills the process with this identifier.

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

Run this twice (one time before creating Word.Application and one time after).

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

Then run 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(); } } } 
+1
source

If you want to close the application for the whole word, you can simply call:

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

Source: https://habr.com/ru/post/897470/


All Articles