When you are talking about office programs what happens when you open a new document seems to depend on how you open it ? and various other factors like was a similar type of document already opened ?
If you are into office programming and you happen to read what Raymond says in one of his recent posts http://blogs.msdn.com/oldnewthing/archive/2006/05/05/590749.aspx I am not sure you will scratch your head first or roll your eyes first. What he says is perfect(and ofcourse he doesn’t need my certificate for that .:)).
Let s have a look :
A document can be executed with no new process being created. The most common case (but hardly the only such) in which you will encounter this is if the registered handler for the document type requested a DDE conversation. In that case, an existing instance of the program has accepted responsibility for the document. Waiting for the process to exit is not the same as waiting for the user to close the document, because closing the document doesn't exit the process.
This generally happens in all of the office documents, and how to you confirm it if you want to ?
Go to any folder->Tools-> Folder Options… -> File Types then click on DOC(or any office ext for that matter) -> Click advanced -> Click on “new” or “open” -> Click “Edit” , you will be presented with a dialog similar to this you can see here actually that its using DDE and the DDE message that is sent actually when a new doc is created (in this case..). but how does it matter .. it does . I have seen many (and I mean many ..) issues in which this knowledge was very useful that what happens when a new doc is opened(let’s talk about one specific type doc..all the other are similar). Let’s look a scenario. You have a document management system(in .NET) that lets the users edit word documents and when they are done editing it, you store the time taken to edit the doc in your database, remember ..you cannot automate word here because your document management system handles 100s of different types of files , so you need to use some generic object that can do the job like process object .
Now the fun starts , when you open the doc just by using obj.Start(<doc_name>) then process is not associated with the obj , here the reason is simple you are actually using a shared method that does not require an actual instance. But then to associate word instance with the process object you use obj.StartInfo.FileName and then say process.start() now its associated, considering that you already enabled raisingevents, it will even fire exited event but only if you don’t have a doc open before you do a obj.start(). If you have then before actually opening the doc it does a DDE broadcast to check if word process already exists ..if it does then it gives the doc to that process..so you are stuck, not you won’t have the exited event that was of primary importance. Now there are many other permutations combinations that are possible. You can implement workarounds but all will have a different catches, and which workaround should you use will purely depend on your situation.
You can tell word not to use DDE..but that will have a negative effect on performance..and that’s not dependable(see the checkbox)
You can start word with "/x" http://support.microsoft.com/?scid=kb;en-us;210565 (This instance of Word responds to only one DDE request and ignores all other DDE requests and multi-instances.)
Killing all the previous instances (saving the doc names and paths )and then opening your own instances and then opening the other docs.
Making word instances child to some form so that they don't respond to DDE broadcast.
And the list goes on….
In these issues the line of thought should be ..what is your problem..? is your instances stolen by other docs..? or you are reusing some other instances that you don't want..
So point here is ..use Automation wherever possible…!!!