Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change which file is active in Word?

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
Using Word VBA, how would I change which file is the active document? I need to have several files open and at a given point, I need to switch and make one of the other files the active document. Is that possible?

Any help is greatly appreciated!

Cheryl
 
Hi Cheryl,

To make a particular document active, use;

Code:
[blue]Documents("[i]Path\Name[/i]").Activate[/blue]
But why do you want to do it? It is not necessary, or even desirable, to activate a document to work with it in code. You would only normally want to do it if you want to bring the document into view for the user.

To work with a document, just do ..

Code:
[blue]With Documents("[i]Path\Name[/i]")
    [green]' Do your stuff with the document[/green]
End With[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
I thought I had to work with the active document. This makes more sense. Thank you for your help!

Any help is greatly appreciated!

Cheryl
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top