Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create exclusive word instance

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.

like image 838
Gpx Avatar asked Sep 21 '10 14:09

Gpx


2 Answers

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.

like image 137
DarinH Avatar answered Sep 22 '22 20:09

DarinH


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.

like image 22
Todd Main Avatar answered Sep 19 '22 20:09

Todd Main