views:

2436

answers:

2

I'm hoping a VB/VBA expert can help me out. Consider the following: The user opens a document in Word 2003, and within the Normal.dot AutoOpen macro, we look at current document, and if it has been opened by clicking on a link on a webpage, and meets certain other application specific criteria, close the streamed 'copy' and open the source document (found on a shared drive we can assume the user has access to):

Documents.Open origDoc
Documents(ActiveDocument.FullName).Close SaveChanges:=wdDoNotSaveChanges
Documents(origDoc).Activate

With ActiveDocument
    ''# Do work
End With

My thought was that I needed to call Activate to ensure that the original document was the ActiveDocument, but I'm getting a 4160 'Bad file name' error on the .Activate call. If I comment out the call to .Activate, it appears that ActiveDocument is set to the origDoc document, even if there were other documents already opened (I'm not really sure how the Documents Collection is managed, and how Word determines what next ActiveDocument would be if you programatically close the current ActiveDocument)

So, does calling .Open on a document explicitly set the Document to be the ActiveDocument? Also, does calling .Activate on the already active document cause an error?

I haven't really been able to find much documentation about this, so thanks in advance for any suggestions and insight!

A: 

You have an error here:

Document(origDoc).Activate

Should be Document**s**.

Yes, you can activate the active document. Nothing happens then.

Yes, opened document becomes active. If you are not sure, use Documents.Open(origDoc).Activate.

GSerg
Thanks for spotting the typo, unfortunately it was only in my post, not the actual code. If you are allowed to activate the open document, then how come I am getting a 4160 error on the .Activate call?
echoesofspring
What exactly does origDoc contain?
GSerg
A: 

You shouldn't be using the ActiveDocument object in the first place unless absolutely necessary because it's very unreliable. The preferred approach would be this:

Documents(ActiveDocument.FullName).Close SaveChanges:=wdDoNotSaveChanges

Dim doc as Document
Set doc = Documents.Open(origDoc)        
With doc
    'Do work
End With
guillermooo