I have Microsoft Word template that I automated filling it's fields from my application, and when the user requests to print I open this template.
But creating the Word application every time a user requests to print after filling fields is very expensive and can lead to some delay while opening the template. So I choose to cache the reference to Word then just open the new filled template.
That solved the performance issue as opening the file is less expensive than recreating Word each time, but this only works when the user just closes the document not the entire Word application. When this happens my reference to Word becomes invalid and returns with exception: "The RPC server is unavailable" upon the next request of opening the template. I tried to subscribe to the BeforeClosing
event but this triggers quitting Word as well as closing documents.
My question is how to know if the Word is closing a document or quitting the entire application so I take the proper action, or any hint for another direction of thinking about improving the performance of opening a Word template.