Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We open the word document programatically using Microsoft.Office.Interop.Word.Application. We update the document and then save it and close the document. It works fine but occassionally it is not able to close the document and quit the application (like if some dialog opens on the document requiring human intervention).

For such situation, we need to kill the word process that was used to open the document so that we can process other documents.

I found this solution file is used by another process in c sharp[^]

Using the code, I am able to find the process and then call process.kill() method to kill the winword process. It deletes the file, however if I try to open the file again, it pops up the File In Use dialog.

How to get away with this? Please help
Posted
Updated 23-Oct-13 5:34am
v2
Comments
Ranjan.D 23-Oct-13 11:38am    
Do you close the application instance like below ?

application.Quit();

1 solution

Using Process.Kill() is not the proper way to shutdown an application.

You may not be properly releasing all of the instantiated COM objects.

Calling a "Quit" or "Exit" method followed by a "Dispose" method on a COM object will not free up all of the references.

See How to Release COM Interop Objects so the Called Program Can Exit[^]

C#
// Call Quit() Method to start Word shutdown process
oApp.Quit();
Application.DoEvents();
//
// This sample has only one COM object declared.
//
// For programs with multiple COM objects declared, repeat the following four
// lines of code for each COM object in reverse order of creation.
//
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp) != 0) {
    Application.DoEvents();
}
oApp = null; // Set each COM Object to null
//
//
// After all of the COM objects have been released and set to null, do the following:
GC.Collect(); // Start .NET CLR Garbage Collection
GC.WaitForPendingFinalizers(); // Wait for Garbage Collection to finish
 
Share this answer
 
v5
Comments
Savy17 23-Oct-13 14:20pm    
I agree that Process.Kill() is not proper way to shutdown an application. There are times when a dialog opens on the document requiring human intervention, we can not programatically close the document and quit the application since it is waiting for user to response to the dialog.
So for scenarios like that, when the document takes longer than x amt of time, we need to forcefully close the document and quit the application that has opened it. For that reason, I was using the code sample given in above link to get the process and then kill it. However it still holds the reference it seems since can't open the same document again as it says file in use until the application is closed and restarted.
Mike Meinz 23-Oct-13 16:22pm    
The most common cause of a dialog box during Word's shutdown process is an unsaved file. Use the NoPrompt option on the Document.Save method to eliminate that possibility.

Word will not shutdown after the .Quit method has been called until you release all of the COM objects that you created in your application program. It may not release the file properly until the COM objects have been released. Please follow the example I provided and then report back on your results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900