Actually, there are a number of reasons why you may want to explicitly work with documents, as objects.
Tony is absolutely correct that it is not best to make a document active. Although I do think
With Documents("Path\Name")
' Do your stuff with the document
End With
is misleading. The above will work if the document is loaded. If you use that with an unopen document, you will get a run-time error. The path/name works with loaded (but not always active) documents.
You can reference the others by using, as Tony suggested, Documents("Path\Name"). This does require you to have the path and name. With the (in my mind) unfortunate way that Microsoft places documents by default, this is a long path. "C:|Documents and Settings\username\My Documents\blahblah.doc".
Also keep in mind that you have to do that full path\name reference every time you want to do something.
But it does work. The alternative is to make them objects.
Say you have three documents loaded, ThisDoc, ThatDoc and TheOtherDoc. Say you loaded them in that order. This means TheOtherDoc was loaded last, and is the active document.
Code:
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim TheOtherDoc As Document
Set TheOtherDoc = ActiveDocument
Set ThatDoc = Documents("Path\Name")
Set ThisDoc = Documents("Path|Name")
Now you can use references to ThatDoc, as in
With ThatDoc
...do whatever
End With
Even better is to make the obejcts as you open the files.
Code:
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim TheOtherDoc As Document
Set ThisDoc = ActiveDocument
' open the next file
Application.Documents.Open Filename:="C:\blahblah\blah\ThatDoc.doc"
' this is now the active document
' so set IT to be an object
Set ThatDoc = ActiveDocument
' open the next file
Application.Documents.Open Filename:="C:\blahblah\blah\TheOtherDoc.doc"
' this is now the active document
' so set IT to be an object
Set TheOtherDoc = ActiveDocument
With ThisDoc
...whatever
End With
With ThatDoc
....whatever
End With
With TheOtherDoc
...whatever
Set ThisDoc = Nothing
Set ThatDoc = Nothing
Set TheOtherDoc = Nothing
ALWAYS, if you set a document as object, rememebr to release the object at the end, by setting it to nothing.
You can name the objects whatever you like, perhaps give them a short name of the actual document name.
Dim Finance1 As Document
Set Finance1 = ActiveDocument
if it is the active document
Set Finance1 = Documents{"Path\Name")
if it is loaded but NOT the active document.
Gerry