switching focus

L

Lloyd

I have created a userform in Word using VBA with several command buttons on
the form. One of the command buttons on the form inserts a custom footnote.
While the code works perfectly well, the command button on the form continues
to keep the focus, not the newly created footnote, where the user is supposed
to enter their text. Is there a routine I can call to set the focus to the
document window where the selection is so the user can automatically start
typing the footnote text immediately after clicking on the command button on
the user form?

Thanks for any help.

Lloyd
 
L

Lloyd

I should add a couple of other bits of information in case it's helpful:
1. The form is running in a modeless state
2. I'm trying not to have to close the form in order to return the focus
the active document window, since that means adding code to show the form
again once the user has completed their immediate task, and the form is
essentially a custom made toolbar which I want to keep open all the time
while the document is open.
3. The current situation requires the user to physically click in the
active document window after running any of the utilities on from the form
before they can actually continue working in the document. This is makes it
less intuitive to the user and slows the process down.
Thanks again for any suggestions on this.

Lloyd
 
D

Doug Robbins - Word MVP

Another way to do it would be to have the button display an input box into
which the user enters the data for the footnote and when the click on OK in
the input box what they entered into the input box would be placed into a
footnote.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Lloyd

Thanks for your suggestion Doug. Unfortunately this only delays the point at
which the user has to make that extra click to activate the document window,
as the form is there only to to provide tools for the user while they work
directly in the document.

What I'm trying to achieve is a seamless ongoing use of the commands on the
form while working in the document, without having to click back into the
document everytime you need use a command.

Thanks anyway. Your suggestion was greatly appreciated.

Lloyd
 
R

Russ

Hi Lloyd,

Doesn't the .activate method return focus to the document?
The .goto method navigates footnotes and other document features.
So after using Doug's suggestion, you could have the document automatically
activated and gone to the footnote it inserted.
 
L

Lloyd

Russ,thanks for offering a thought on this. Actually, the .activate method
was one of the first things I tried. But I've not been successful with it.
I believe the .activate method isn't working because it seems that the form
is part of the same object that the activate method is being called from,
such as "ThisDocument.Activate". And ithe activate method doesn't seem to be
able to distinguish between different parts of the "ThisDocument" object. So
it appears the activate method is merely refreshing the focus on whatever
already has the focus in the active window, namely the form.

Perhaps an API call is necessary here. I'm playing around with some code
for that at present. If I come up with anything that works I'll be sure to
post it. Nevertheless, I do appreciate your input and any other thoughts you
might have on the subject.

Lloyd
 
L

Lloyd

Thanks a million, Tony. That worked perfectly. I could kick myself for not
trying that. The application object is not as high up on the chain as I
thought it was. I assumed the application object was Do you by any chance
know where I can get a copy of the full VBA object model tree? It would be
hugely helpful.

Thanks again for zeroing in on the solution.

Lloyd
 
T

Tony Jollans

I think one needs to be very careful with terminology here to avoid
confusion (as best one can). The Application object is top of the tree, or
perhaps top of *a* tree and the trick to understanding this is to recognise
that a non-modal form is essentially separate from the application as far as
Windows is concerned.

The object model should be in Help but wouldn't really have helped with this
as it only shows Word objects; your own objects are not part of the same
tree.
 
R

Russ

Lloyd,
You need to use Application.Activate, not ThisDocument.Activate.
Especially not ThisDocument, since ThisDocument may refer to a underlying
template containing vba modules that created your currently open working,
non-template document. So if your template wasn't currently open in Word for
editing, then ThisDocument.activate would not bring the template or any
other document to the forefront.

Below is quote from VBA Help:

"Activate Method Example
This example activates the document named "Sales.doc."
Documents("Sales.doc").Activate
This example activates the next window in the Windows collection.
ActiveWindow.Next.Activate
This example splits the active window and then activates the first pane.
With ActiveWindow
.SplitVertical = 50
.Panes(1).Activate
End With"

I thought you would read that and choose the first example and activate your
working document by name, assuming that you had captured the name earlier
with ActiveDocument.Name when it did have focus.
 

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