PDA

View Full Version : How do I set focus back to word?



whytedawg
11-14-2007, 03:40 AM
I have a dialog that is modeless, and I wish it to work so that when the user clicks on a button, the focus is returned back to the word document so they dont have to do click the word window again.

A workaround I have found is this:

Application.Windows(1).Visible = False
Application.Windows(1).Visible = True

By hiding and showing it, it sets the focus back to the window...but its a little flickery...was wondering if anyone knew of a better way because the SetFocus and Activate function doesn't seem to do the trick.

Thanks

OTWarrior
11-14-2007, 03:56 AM
how is it leaving the focus of word document?

does this help?

Word.Application.Activate

whytedawg
11-15-2007, 03:14 AM
Well the focus leaves because when you click on the dialog button, i guess its the same as clicking on the dialog itself, and so the 'focus' is directed onto the dialog. The function you provided me with makes word behave strangely...it kind of locks up the input until i switch to another program and then back to word again...totally weird!

OTWarrior
11-15-2007, 04:03 AM
have you tried to close the dialog or make it not visible?

Any chance of an example document with your code in?

whytedawg
11-16-2007, 04:11 AM
Hmm u cant upload files on this forum can u...know of any good free hosting places? Like imageshack but for files ?

Um basically the steps to reproduce:

Go to vba editor
Insert a form
Insert a module
write something like


Sub Test()
UserForm1.Show(false) ' make it not modal
end Sub


Add a button to your userform, and double click it. In here write

CommandButton1.Text = Selection.Text
Now run the module by clicking 'tools/macros/run'

Now, say i wanted it so after clicking the button you could continue to type in word...how could I do that :)

This is a silly example...my program does something a lot more advanced, its just in terms of losing and gaining focus, this seemed like a simple example that holds close to what i require. Thanks.

OTWarrior
11-16-2007, 05:52 AM
you can upload on this forum as a zip (there are heavy limits on other file types).

I will have a play with that code and get back to you.

OTWarrior
11-16-2007, 07:10 AM
I just tried your code with:
Word.Application.Activate
as the button event, and it works fine.
Try the example I have posted and tell me if that is ok.

fumei
11-16-2007, 07:49 AM
This is very odd code.CommandButton1.Text = Selection.Text