PDA

Click to See Complete Forum and Search --> : Problems with word


Heike
Jun 5th, 2002, 03:22 AM
I am developing a COM AddIn for Microsoft Word using Visual Basic .NET.

1) How can I open an existing document or add a new document without getting a new word instance? My code looks like the following at the moment:

objWordInstance.Documents.open(Filename:=mstrDateinameMitPfad)

and

objWordInstance.Documents.Add()

When I open Word (I am using Word 2000) and immediately open my document everything works fine. But when I have already an open document that statement opens my document within a new word instance. Adding a document always opens a new word.

2) Is it possible to recognise that a document is closed? I know the event DocumentBeforeClose but I really need to know that the document is closed.

Can anybody help?

Frans C
Jun 5th, 2002, 06:53 AM
Word 2000 is not an MDI application anymore. You cannot open multiple documents in the same instance of Word; not with VB and not with Word. However, multiple instances of word will run in the same process. So if you open a modal userform, it will be modal to all instances of Word, and not only to the instance that opened the userform. So the short answer to question 1 is: You can't

Heike
Jun 5th, 2002, 09:15 AM
Thanks, that helps.
But I then have another question. I open word and a "Dokument1" is opened. Within my VB-Code I add a new document. A new window is opened. Using objWordInstance.ActiveDocument.SaveAs(mstrDateinameMitPfad) I can give that new document a new name, but after that the window with "Dokument1" is still active, even when I say objWordInstance.windows(mstrDateiname).activate(). How can I make my new document being active???

Frans C
Jun 5th, 2002, 09:35 AM
As soon as you add the new document, you could assign it to a variable. Whenever you need a reference to this document, you can use this variable, instead of objWordInstance.ActiveDocument.

eg.
Set objDoc = objWordInstance.Documents.open(Filename:=mstrDateinameMitPfad)

Now you don't have to refer to objWordInstance.ActiveDocument anymore, because you have a direct reference to the correct document

You could use:
objDoc.SaveAs(mstrDateinameMitPfad)
and you are sure it is the document you opened, and not some document the user happened to activate manually.

If you want, you could activate it, but this will only be needed if you wanted the user to see it.
objDoc.Activate