How to improve performance of opening Microsoft Word when automated from C#?
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 whil开发者_如何学Pythone 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.
I'm unfamiliar with how to properly subscribe to the Word closing events. But it seems like one option you could take is to optimistically assume that Word is not closed and use your caching route. At a high level catch the Exception
type thrown when Word is closed. If you encounter this exception, remove your reference to word and repeat the operation like it was happening for the first time.
You won't get the caching all of the time this way. But you will be able to take advantage of situations where the user only closes the document.
精彩评论