Search code examples
language-agnosticms-officeoffice-automation

Check for hung Office process when using Office Automation


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?


Solution

  • 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.

    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.

    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.

    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.

    Here are some commands to use in a batch file for cleaning up (both should be in the path):

    • sc stop servicename - stops service named servicename
    • sc start servicename - starts service named servicename
    • sc query servicename - Queries the status of servicename

    • taskkill /F /IM excel.exe - terminates all instances of excel.exe