Force a specific Window to have focus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an Access application that merges various word documents. Normally
this works as expected and the new document is displayed in a Window and that
window has the focus.

However, part of the process checks for a duplicate file name. If the
duplicate is found then a message is presented by Access. After the message
is presented and the user has completed their interaction with the
application, the Word document is merged and displayed. However, the focus
remains in the Access application.

What code can I use to force the focus to the new merged Word document Window?

Thanks for any assistance.

Mr B
 
To activate Word, use:

objWORD.Activate

where objWORD is the object variable pointing to the Word application
object.

If you need to activate Access, use the "SetForegroundWindow&" API function,
as follows.

1. Declare the Windows API function in the general declarations section
(at the top of the module) like so:

Declare Function SetForegroundWindow& Lib "user32" _
(ByVal hwnd As Long)

2. When you want to activate Access in your code, write the statement:

SetForegroundWindow& Access.Application.hWndAccessApp

Geoff
 
Geoff,

Would it be accurate to say that if Access has the focus that I could use
the API function the same way, only refer to Word like:

SetForegroundWindow& Word.Application.hWndAccessApp

Mr B
 
Would it be accurate to say that if Access has the focus that I could use
the API function the same way, only refer to Word like:

SetForegroundWindow& Word.Application.hWndAccessApp

No, I'm afraid not - you cannot use the above code line in quite the same
way. The reason is that the object models for Access and Word are
different.

The object model for Access includes the method hWndAccessApp, which is
called from Access's Application object (as in
Access.Application.hWndAccessApp). The hWndAccessApp method returns a long
integer, which is the number Windows has assigned to the Access window.

In contrast, the object model for Word does not include a "hWndAccessApp"
method (or, as you probably meant to say, a "hWndWordApp" method). Therefore
you cannot write "Word.Application.hWndWordApp" to obtain the long integer
number Windows has assigned to the Word window.

As you have no doubt realised, you must pass a long integer that points to
the window you want to activate to the API function.

If you have Intellisense turned on, the presence or absence of methods
(properties and events) will become more obvious to you. For example, when
you type in the VBA editor "Access.Application." (without the quotes and
with the full stop at the end), the method "hWndAccessApp" appears in the
pop-up (Intellisense) list; however, when you type "Word.Application.",
there is no "hWndWordApp" method for you to select in the pop-up list.

You can search the object models for individual object libraries by pressing
F2 to open VBA's Object Browser. In Object Browser, you can select one
object library (or all object libraries), enter some search text and click
the binoculars to search the library. After highlighting an item, you can
press F1 (or click the question mark on the toolbar) to get help on the
item.

You may think this is somewhat inconsistent but it is easier to write:

Word.Application.Activate
or
objWORD.Activate

than to determine window handles.

The Word object model includes the Activate method for a number of objects
(eg the document object), not just the Application object. This means you
can activate not only Word (ie the Word Application), but elements within
Word. For example, you might want to point a document object variable to
one document and then activate it; similarly, you might want to point a
different document object variable to a different document and close it
without saving changes (typical after mail merges). It seems the Word
object model is designed to give you, the developer, the flexibility you
need to manipulate Word objects.

Geoff.
 

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

Back
Top