Problem with interop.word in a .net application

▼魔方 西西 提交于 2019-12-12 06:15:51

问题


Solution Environment:

MS Visual Studio 2010 Ultimate, MS Office 2010 Professional, Microsoft Interop classes

Scenario:

I've created a .Net application which uses the Interop classes to interact with MS Word. Consider this: An application which mimics a key-value pair table. When the user clicks on the "value" field, the .net application opens up a winword window where the user can enter some notes. Upon clicking close (winword), .net app will do a winword.wordopenxml and save the data in the field. For this to happen, i've added event handles to winword. The application runs perfectly fine. The problem arises when i have multiple instances of winword opened (separately by the user but not thru the .net application) . The event handlers from the .net application is applied to all instances of winword and hence, upon closing just one instance word, all the other instances gets closed too. I tried different ways to initialize the word object in the application. I also tried the work around "Initializing two instances of winword and quit the first instance" -> This approach also did not work well.

Gurus: Could you please help me find a suitable solution for this problem ?

-Rupesh


回答1:


This is not what is really going on. It may look like you have multiple instances of Word running but there really is only one. Word is a very large program that requires a lot of system resources. When the user starts another copy, that copy checks if Word is already running and asks it to open the document. And quits. The original instance just creates another window, it otherwise indeed looks exactly like another instance of Word.

So when you make Word exit, you kill all the other windows as well. Use Taskmgr.exe, Processes tab to verify this. As a workaround, consider enumerating the Documents collection to see what else is opened.



来源:https://stackoverflow.com/questions/6317829/problem-with-interop-word-in-a-net-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!