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.
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
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With