<p>Is there a way to check to see if an Microsoft Office process (i.e. Word, Excel) has hung when using Office Automation? Additionally, if the process is hung, is there a way to terminate it?</p>

[{'answer_id': 9947, 'author': 'Ed Schwehm', 'author_id': 1206, 'author_profile': 'https://Stackoverflow.com/users/1206', 'pm_score': 0, 'selected': False, 'text': '<p>I can answer the latter half; if you have a reference to the application object in your code, you can simply call "Quit" on it:</p>\n\n<pre><code>private Microsoft.Office.Interop.Excel.Application _excel;\n// ... do some stuff ...\n_excel.Quit();\n</code></pre>\n\n<p>For checking for a hung process, I\'d guess you\'d want to try to get some data from the application and see if you get results in a reasonable time frame (check in a timer or other thread or something). There\'s probably a better way though.</p>\n'}, {'answer_id': 9952, 'author': 'RobertTheGrey', 'author_id': 1107, 'author_profile': 'https://Stackoverflow.com/users/1107', 'pm_score': 1, 'selected': False, 'text': '<p>I remember doing this a few years ago - so I\'m talking Office XP or 2003 days, not 2007.</p>\n\n<p>Obviously a better solution for automation these days is to use the new XML format that describes docx etc using the System.IO.Packaging namespace.</p>\n\n<p>Back then, I used to notice that whenever MSWord had kicked the bucket and had had enough, a process called "Dr. Watson" was running on the machine. This was my first clue that Word had tripped and fallen over. Sometimes I might see more than one <em>WINWORD.EXE</em>, but my code just used to scan for the good Doctor. Once I saw that (in code), I killed all <em>WINWORD.EXE</em> processes the good Doctor himself, and restarted the process of torturing Word :-)</p>\n\n<p>Hope that gives you some clues as to what to look for.</p>\n\n<p>All the best,</p>\n\n<p>Rob G</p>\n\n<p>P.S. I might even be able to dig out the code in my archives if you don\'t come right!</p>\n'}, {'answer_id': 20225, 'author': 'bruceatk', 'author_id': 791, 'author_profile': 'https://Stackoverflow.com/users/791', 'pm_score': 3, 'selected': True, 'text': "<p>Let me start off saying that I don't recommend doing this in a service on a server, but I'll do my best to answer the questions.</p>\n\n<p>Running as a service makes it difficult to clean up.  For example with what you have running as a service survive killing a hung word or excel.  You may be in a position to have to kill the service.  Will your service stop if word or excel is in this state.  </p>\n\n<p>One problem with trying to test if it is hung, is that your test could cause a new instance of word to startup and work, while the one that the service is running would still be hung.</p>\n\n<p>The best way to determine if it's hung is to ask it to do what it is supposed to be doing and check for the results.  I would need to know more about what it is actually doing. </p>\n\n<p>Here are some commands to use in a batch file for cleaning up (both should be in the path):</p>\n\n<ul>\n<li>sc stop servicename  - stops service named servicename</li>\n<li>sc start servicename - starts service named servicename</li>\n<li><p>sc query servicename - Queries the status of servicename</p></li>\n<li><p>taskkill /F /IM excel.exe - terminates all instances of excel.exe</p></li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/9905', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1185/']