PDA

View Full Version : Solved: Detect activation of a userform



DChinell
03-06-2009, 01:54 PM
Does anyone know how I can detect the activation of a userform in VBA (Word 2000).

I have a modeless userform displayed. The user leaves the form and performs some operation in the underlying document. Then the user returns to the userform and continues using it.

How can I detect when the user activates the userform? The Userform_Activate event isn't the answer as it only runs when the form is first displayed.

Any ideas short of putting a click event almost everywhere and hoping the user clicks the body rather than the caption (title bar) of the userform?

Bear

Oorang
03-10-2009, 06:52 PM
To use the userform activation event, put this in the code of the userform:
Private Sub UserForm_Activate()
'Do something here
End Sub

DChinell
03-11-2009, 06:13 AM
Oorang: Thanks for trying to help me, but what you propose is exactly what doesn't work. In VBA for Word 2000 the UserForm_Activate event only occurs when the form is first initialized and displayed. If, after being displayed I transfer focus to the document, then back to the UserForm, the event does not fire.

Here's an example of the code I tried to use:

Private Sub UserForm_Activate()

' The user has changed the selection. This
' repeats appropriate portions of the
' initialization routine.

' Set the working range to the selection

Set objWorkRange = Selection.Range

' Initialize the control states

Me.cmdFindNext.Enabled = True
Me.cmdSetFormat.Enabled = True
Me.cmdCancel.Enabled = True

With Me.cboFindStyleName
.SelStart = 0
.SelLength = .TextLength
.SetFocus
End With

End Sub

Any other ideas?

Bear

CreganTur
03-11-2009, 06:37 AM
How are your users getting back to the form? Are they clicking on the form (in which case you can use the UserForm_Click Event) or are they clicking on the related tab in the taskbar?

DChinell
03-11-2009, 07:04 AM
Randy: The form is nonmodal. During an operation they may want to change the selection in the underlying document or make minor edits. They typically return to the form by clicking its title bar or the form body.

So, yes, the UserForm_Click event would work (and does, and I'm currently using it to fire the UserForm_Activate code) but that doesn't cover the instance when the user clicks the title bar, or any of the controls for that matter.

Bear

DChinell
03-11-2009, 07:07 AM
PS: I've even tried a ThisApplication class module, but neither the WindowSelectionChange nor the WindowActive events seem to "see" the userform.

lucas
03-11-2009, 07:11 AM
I have to ask, what do you want to happen when the form is re-selected?

Oorang
03-12-2009, 06:01 AM
Then you are probably going to have to hook the window event instead. In the ThisDocument Module put this:
Option Explicit
Private Sub Document_Open()
Set g_clsAE = New AppEvents
End Sub

Put this in a standard module:
Option Explicit
Public g_clsAE As AppEvents
And finally in a class module that you named "AppEvents" put this:
Option Explicit

Private WithEvents wrd As Word.Application

Private Sub Class_Initialize()
Set wrd = Word.Application
End Sub

Private Sub wrd_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
'do something
End Sub

DChinell
03-12-2009, 09:36 AM
Aaron: Thanks! That looks promising. I'll have to give it a shot, but when it comes to class modules, I have to work up my courage, as they're a little beyond my comfort level at present. For now, I'll consider this issue resolved.

Bear

Oorang
03-16-2009, 05:44 AM
Class don't have to be scary, just think of them as your own custom objects:) (Or UDT 2.0)