Auto run a macro on switching documents

  • Thread starter Roderick O'Regan
  • Start date
R

Roderick O'Regan

I'm using Word 2007 with Vista.

I've created a template which, when run, allows the use, through a
userform,r to set the left margins either to 2cm ("Narrow") or 7cm
("Wide").

The user has created two documents based on the above template and
which are open on their desktop. One has wide margins and the other
narrow ones based on the settings above.

In the template VBA Editor under "This Document" folder I've created a
macro when run checks the value of the margins for the active document
and tells the user via a msgBox whether it is "Wide" or "Narrow".

This works OK for the two open documents but on a manual basis only.

What I now want to do is automate this so that when the user changes
focus from one document to another it automatically runs the macro
which tells them whether they are working in the document with either
wide or narrow margins set.

I've been trying to see if there is anything to be found on
automatically updating macros or even the SetFocus command but to no
avail. I suspect I'm not using the correct keywords!

Is my automation request possible, please?
 
R

Roderick O'Regan

Funny what one can find if you dig deep enough...!

Further searching came up with this article contributed by David Rado
acknowledging Bill Coan.

It can be found at this link:
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

As this only has to happen with one particular template rather than
globally, I created the class module "ThisTemplate" within the
template.

It has the following code within it:
Option Explicit
Public WithEvents oApp As Word.Application
------------------------
Private Sub oApp_DocumentChange()
Dim ThisMargin
ThisMargin=ActiveDocument.CustomDocumentProperties("MarginSet") --
<should be either WIDE or NARROW>
MsgBox ("This document is set to have " + ThisMargin + " margins")
End Sub
=====================

In the class module 'This Document' of the template I then created the
following:
Option Explicit
Dim oAppClass As New ThisTemplate
----------------------
Sub AutoExec()
Set oAppClass.oApp=Word.Application
End Sub
(Comment: I clicked within the above procedure and pressed F5 to
register it)
===================

Saved everything. Ran the template twice creating new documents: one
with wide and the other with narrow margings.

Switched between the documents and...
Zippo. Nothing. Nada!

I seem to have done something wrong here but cannot spot it.

Can anyone offer any suggestions, please?
 
N

Neil Humphries

I can't help you with the VBA, but are you sure you want the user to have to
read and close a message box every single time the focus changes to the
document?

As a user, it would get really tiresome very quickly to have to keep closing
the message box. Can you not achieve the same effect by either displaying the
ruler or changing the view to page layout so the margins are visible?

Regards,
Neil
 

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