PDA

View Full Version : Solved: User Form causing docs to steal focus



Gavint
09-12-2006, 07:18 PM
I have a user form that when run more than once, causes Word to change focus back to the original document that it was run on. :banghead:

For example, I start with document 1, run the form all goes well. I then open document 2, run the form again and as soon as the form shows, the focus is taken back to document 1.

Through debgging, the focus is changed as soon as the .show line is run.

I'm using frmName.show to launch the form and I've tried Me.Hide and Unload to close the form with no luck.

fumei
09-12-2006, 08:58 PM
WHICH document has the form??? Is the form in Document A, or is it Normal.dot, or is it in a template that made Document A.....

Post your code.

Gavint
09-12-2006, 09:12 PM
The form is launched from a macro in a global template (effectively like normal.dot).

I've done a bit of further reading and found info on declaring the form first and opening a copy of it (I've had no training, so I've just been doing form.show). Here's a cut of code from another forum.

Dim MyUserForm As frmSignoff
' create a copy of the form
' an assign it to the variable
Set MyUserForm = New frmSignoff
' pass control to the form
MyUserForm.Show
' when the form closes, control
' returns to here... destroy it
Set MyUserForm = Nothing

This seems to fix the immediate problem, but appears to be rather excesive.

Further, it clears the data previously entered in the form, which is actually useful to retain in this case (as it's a fairly repetative process).

fumei
09-13-2006, 11:38 AM
I can not duplicate this, so you are going to have to details EXACTLY the steps you are taking.

I have forms that are in a global template. I show the form. I close the form. I switch to a different document and show the form. Focus is not switched to the previous document.

1. Is there some activity started by the form that is not completed when you switch to the other document?

2. Describe precisely your use of .Hide, or Unload.

Gavint
09-14-2006, 12:23 AM
Basic Setup:

Global template which sits on the local PC.
Word is loading template via file locations (startup folder)
Macro is launched from toolbar buttonMacro:

The macro code is very basic, for the purposes of displaying the issue, all I need to run is the following

Sub SignOff()

frmSignoff.Show

end sub


In the form itself most of the code is in relation to clicking something. eg OK button:

Private Sub cmdOK_Click()
If frmSignoff.txtPrimary = "" Then
MsgBox ("You must enter at least the primary operator's intials"), , "Operator's Initials"
frmSignoff.txtPrimary.SetFocus
Else
If frmSignoff.optDefault = False And frmSignoff.optYF = False And frmSignoff.optYS = False And frmSignoff.txtValediction = "" Then
MsgBox ("You must enter a valediction"), , "Valediction"
Else
Me.Hide
End If
End If
End Sub

It has got me stumped. It's also occurring on other forms we have developed and on all workstations (XP SP2 with Office 2003).

fumei
09-14-2006, 08:26 PM
If you use .Hide, yes when you .Show again focus will return to the original document.

Are you saying - as you seem to be - that if you actually UNLOAD the form, go to another document (not closing the first one), and load the form again the focus returns to the first document?

BTW: could you please use the underscore character when you post code here? Not using it on long lines makes it difficult to read. The code window is stretched left/right, which means you have to scroll left/right AND up/down in the code window.

Not only that, but in your own code windows - properly indented - it makes it easier for YOU to read.

EXAMPLE:If frmSignoff.optDefault = False And _
frmSignoff.optYF = False And _
frmSignoff.optYS = False And _
frmSignoff.txtValediction = "" Then Thanks.

Gavint
09-20-2006, 09:38 PM
Thanks for the feedback.

For those looking for the actual answer to my problem one solution is to simply create a new instance of the form each time.

Set userForm = New userForm
userForm.show

Previous values on the form will be lost, but they can be stored/retrieved before intialisation if required.