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.