Programatically activate the previously active document?

L

Larry

Would there be some way to be able to go automatically to the previously activated document?

Please note: I'm not speaking of the previous document in the Window menu, which is activated by the PrevWindow command, but of the previously active document, wherever it may happen to be in the Window menu.

To put it another way, does Word created some "record" of the currently active document, so that the user can access that "record" and return to that document?

I'm working with Windows 97, which have a "open document" event that can be accessed with the AutoOpen macro, but it doesn't have an "active document" event. And that's what I need. In other words, every time a document is activated, the address of that document would be automatically recorded, and then it could be accessed by a macro. .

By the way, I have a set of two macros I created that enable me to go back and forth between any two documents. It's very handy, but using it requires me to run one macro before leaving the first document. This "sets" that document, and when I've gone to a second document, I run the second macro which activates the first document while also setting the second document so I can return to it. But what I want is something simpler whereby I don't have to set the first document but can with one step return to the previously active document.

Thanks.

Larry
 
C

Cindy M -WordMVP-

Hi Larry,

Word 97 has an extremely limited number of events available, and I don't have it
installed anywhere anymore. What APPLICATION level events does it offer, could you list
them for us? I expect it has one that can be made to do the job...
Would there be some way to be able to go automatically to the previously activated document?

Please note: I'm not speaking of the previous document in the Window menu, which is
activated by the PrevWindow command, but of the previously active document, wherever it
may happen to be in the Window menu.
To put it another way, does Word created some "record" of the currently active
document, so that the user can access that "record" and return to that document?
I'm working with Windows 97, which have a "open document" event that can be accessed
with the AutoOpen macro, but it doesn't have an "active document" event. And that's what
I need. In other words, every time a document is activated, the address of that document
would be automatically recorded, and then it could be accessed by a macro. .
By the way, I have a set of two macros I created that enable me to go back and forth
between any two documents. It's very handy, but using it requires me to run one macro
before leaving the first document. This "sets" that document, and when I've gone to a
second document, I run the second macro which activates the first document while also
setting the second document so I can return to it. But what I want is something simpler
whereby I don't have to set the first document but can with one step return to the
previously active document.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 
L

Larry

I haven't yet found a list of Application Events (the ones I know of are AutoOpen, Autoclose etc.)

But look at this, found in Help, under the heading, "Activate, DeActivate Events. It's not clear whether the objects it works with include a Word document.


The Activate event occurs when an object becomes the active window. The Deactivate event occurs when an object is no longer the active window.

Syntax

Private Sub object_Activate()
Private Sub object_Deactivate()

The object placeholder represents an object expression that evaluates to an object in the Applies To list.

Remarks

An object can become active by using the Show method in code.

The Activate event can occur only when an object is visible. A UserForm loaded with Load isn't visible unless you use the Show method.

The Activate and Deactivate events occur only when you move the focus within an application. Moving the focus to or from an object in another application doesn't trigger either event.

The Deactivate event doesn't occur when unloading an object.
 
C

Cindy M -WordMVP-

Hi Larry,
I haven't yet found a list of Application Events (the ones I know of are AutoOpen, Autoclose etc.)
These aren't events...

Activate/Deactivate applies only to UserForms, as you can see if you actually look in "Applies to".

OK, I dug out the dusty box where I keep old manuals, among others the entire Office 97 VBA
Reference. That version had (has) two event: DocumentChange and Quit. Try searching DocumentChange
in the Word 97 Help - that's going to be your only hope (short of upgrading to a newer version).

It would probably be best to use an INI-type of file (see PrivateProfileString) with two entries:
one for the "active document" - that would be ActiveDocument when DocumentChange fires; one ofr the
previous document - when DocumentChange fires first write the value of "active document" to
"previous document", then write the ActiveDocument to the value of "active document".

The macro that switches back then checks the value of "previous document" and activates that
document's window. More or less. The devil is always in the details.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and
not by e-mail :)
 
L

Larry

Thanks. Yes, I saw it dealt with User forms, but since it referred to objects, and a document is an object, I didn't want to exclude the possibility that it might apply to documents as well, though I didn't think it likely.

And of course AutoOpen is not an event, it is a macro, but it is run by an event, the opening of a document.

Sorry for my less-than-clear writing.

Larry
 
L

Larry

Cincy,

I followed the directions in Help for DocumentChange Event and for Using Events with the Application Object. I created a Class Module called Class! in the Normal template and it now looks like this. I stuck in the msgbox as a test to see if it ran when I activated a new document, but nothing happened. Maybe the code that is to be triggered by the Event is supposed to be placed elsewhere?

Thanks,
Larry


Public WithEvents App As Word.Application


Private Sub App_DocumentChange()

MsgBox "Hello."

End Sub

Private Sub App_Quit()

End Sub

Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
 
C

Cindy M -WordMVP-

Hi Larry,
I followed the directions in Help for DocumentChange Event and for Using Events with
the Application Object. I created a Class Module called Class! in the Normal template
and it now looks like this. I stuck in the msgbox as a test to see if it ran when I
activated a new document, but nothing happened. Maybe the code that is to be triggered
by the Event is supposed to be placed elsewhere?The following needs to be in a "plain vanilla" module, not in the class module. Note my
change to the first line. When you type Dim X As New a list of possiblities should
appear, and your class name should be in the list. If you don't find it, there's a
problem :) Then you have to click in Sub Register_Event_Handler and press F5 to activate
the application level events in your class.

Dim X As New Class!

Sub Register Event Handler()
Set X.App = Word.Application
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top