how to return focus back to Access

A

aprice

I have a VBA script that retrieves attachements from
Outlook emails. It works fine if Outlook is not running.
However, if Outlook is running, Outlook gets the focus
(Outlook is the active window), and I am unable to return
back to Access without clicking on the taskbar.

Is there a vba command that will automatically return the
focus back Access? I have tried SendKeys with ALT+TAB, but
this screws up is Outlook is not running but something
else is like Word. The end result is I ALT+TAB to Word.
 
D

Dan Artuso

Hi,
Have you tried simply setting the focus to an open form or control
on any open form after your code has run?

If that doesn't work, you could give this slightly more complicated method a try :)

Place all this in a standard module:

Public Const HWND_TOP = 0
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_SHOWWINDOW = &H40

Public Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long,
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)

'note, the above is all on one line

Then place this immediately after your code:
SetWindowPos Application.hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE

the above should all be on one line

I've used this successfuly in a VB app that interacts with Outlook.
 

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