views:

122

answers:

2

Hi there,

i am creating a word (14) instance with interop out of a c# .net4 winforms application to work with a document. If some word document gets opened beyond my application the same word instance will be used an disturbs my application.

Simple question: Is there any way to set my word instance exclusive for my application?

Thanks in advance.

Btw: Found some stuff with exclusive/word/office/isolated/block/instance but no answers anyhow.

+4  A: 

No, there is no way for you to lock an instance of Word just for yourself.

But, based on your comment, it's easy to work around the issue - don't use ActiveDocument. You can get around using ActiveDocument by calling the document something specific and then using that variable (whether you are opening an existing doc or creating a new one).

For example:

Sub NewDoc()
    Dim d As Document
    Set d = Documents.Add(Visible:=False)
End Sub
Sub ExistingDoc()
    Dim d As Document
    Set d = Documents.Open(FileName:="C:\myexisting.doc")
End Sub

In both cases above, you'd just use d in place of where you used to use ActiveDocument.

Otaku
Thanks Otaku, but it´s not my app who is opening the second document. I´m fine with my word handling, but a user outside of my app is trying to open a word doc and the system will automatically use my invis instance. So i want to block my instance just for me and the user should get a new instance.
Gpx
@Gpx: So the second document opened by the user is also opening invisible (i.e. they can't see their document)?
Otaku
No, the second document is opening my existing instance visible with the new document as active document. So my app is trying to interact with the instance but the wrong document. So i´ll either try to stop my word instance from opening a new doc or set a option to always open docs in a second instance.
Gpx
Again, in the first sentence, "No, there is no way for you to lock an instance of Word just for yourself." That being said, unless you're trying to make your document remain invisible, you can stop your code from interacting with the newly opened second document by not using `ActiveDocument`, as stated above.
Otaku
ActiveDocument is being used by Word internally when processing MergeMail functionality. Opening a new document in the word instance which is busy with MergeMail results in an error, because it is Word depending on ActiveDocument and not us. We need a way to stop the word instance opening documents and changing it's ActiveDocument while it is busy with MergeMail.
naacal
+1  A: 

There's sort of a solution, but it's not pretty. The main issue is that Word registers itself in the ROT (Running object table), and other applications can then easily get access to the instance of Word registered in the ROT (that's what the VB GETOBJECT function does for instance).

So, in your app, you'd basically have to do 2 things

1) Try to GETOBJECT (ie query the ROT for a running instance) 2) If you get one, you know you HAVE to create a new instance of Word to use (CREATEOBJECT in VB, the process is different in other langs). 3) If you DON'T get one, you have to create 2 new instances of Word. The first will automatically register itself in the ROT, the second won't. Use the second instance, and quite the first instance.

Even though you terminate that first instance, It won't "retroactively" register itself in the ROT, and other applications will generally not object a reference to it to use, they'll automatically create a new instance, which, since no other instance is registered in the ROT anymore, will then get registered.

That said, it is still possible for other apps to get at your instance of Word, so this technique isn't bulletproof. How? Because Word ALSO registers each loaded DOCUMENT in the ROT. But that's a pretty seldom used feature.

drventure