Give textbox focus when form loads modeless

E

Ed

Hello all,

Using Word 2003. I have a VBA userform which contains a textbox
(txtFindText) and some buttons.

When the form is displayed I would like the textbox to have the focus.

I have a line - txtFindText.SetFocus - in the form's initialisation handler
and this seems to work when I invoke the form by "MyForm.Show".

However, if I invoke the form by "MyForm.Show vbModeless" the textbox does
not seem to get the focus and I seem to need to press the Enter key to give
the box the focus.

Is there a way to give the textbox the focus when the form is displayed as
modeless?

Thanks.

Ed.
 
B

Bear

Ed:

I had the same problem in Word 2000. After showing the form, I had to set
focus on a different control, then set it back to the control in question (in
this case a combo box. I hope this will work with a text box too. Here's the
code for the subroutine. The workaround is marked.

Sub ReplaceStyles()

' Replaces paragraph or character styles

If Not objReplaceStyles Is Nothing Then
Set objReplaceStyles = Nothing
End If

Set objReplaceStyles = New frmReplaceStyles

objReplaceStyles.Show

' Workaround to set focus in modeless form

objReplaceStyles.cmdCancel.SetFocus
objReplaceStyles.cboFindStyleName.SetFocus

End Sub

Bear
 
E

Ed

Hi Bear,

Your example code helped solve my problem :)

It looks like 2000 and 2003 may behave slightly differently. I originally
had the SetFocus in the UserForm_Initialize handler. I tried switching the
focus to another control and then to the textbox but it didn't make any
difference. So I moved the SetFocus out of the handler into the calling
macro. This worked. I then removed the "dummy" SetFocus and just set the
focus to the textbox and this worked as well. So it looks like in 2003 it
just needs to be done by the calling macro rather than by the form.

Thanks for the help.

Regards.

Ed
 
B

Bear

Ed:

REALLY glad I was able to help out. I spent a lot longer on this problem
than I wanted to, and ended up with a workaround for functionality that
should have worked. By the way, that code I sent you IS the calling macro. So
my workaround code is in the calling sub, just like yours.

Bear
 
E

Ed

Hi Bear,
I spent a lot longer on this problem than I wanted to

Tell me about it!
By the way, that code I sent you IS the calling macro. So my workaround code is >in the calling sub, just like yours.

Yes, I wasn't being clear. What I meant was that it seems that in 2000 you
need to switch focus from one control to the one you want whereas in 2003 you
don't seem to need to do that; simply moving the relevant code to the calling
macro (and qualifying the control name with the form name) seems to be
sufficient. Progress? Maybe. (I haven't tested with 2007 yet :-|)

Thanks again.

Regards.

Ed
 

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