Outlook Automation

S

SHIPP

How do I reset the focus back to Access after sending an email message
through automation? The focus remains on Outlook. I am using Access 2003.
 
T

Tom van Stiphout

On Thu, 31 Jan 2008 09:13:01 -0800, SHIPP

If you're using Automation you don't even need to show Outlook. The
CreateObject call will create an invisible object. Developers
sometimes write "objOutlook.Visible=True" but you don't have to.

Additionally you can use the ShowWindow API to set focus to an
application. To get the window handle of MsAccess, use
application.hWndAccessApp

-Tom.
 
G

GeoffG

Here's some code you can adapt.
Check that it works as you expect on all platforms.

Regards
Geoff



Option Compare Database
Option Explicit


' This code is for Microsoft Access.


' Declare API function:
Public Declare Function SetForegroundWindow Lib "user32" ( _
ByVal hWnd As Long) As Long


Private Sub DemoShowAccess()

' Shows Access Application after
' Automation work completed.

Dim fSuccess As Boolean

On Error GoTo Error_DemoShowAccess


'****************************
' Do Automation work here.
'****************************


' Call the ShowAccess() function (below):
fSuccess = ShowAccess(True)
If Not fSuccess Then
GoTo Exit_DemoShowAccess
End If

' Show message in Access window:
MsgBox "Finished"

Exit_DemoShowAccess:

Exit Sub

Error_DemoShowAccess:

MsgBox Err.Description, vbOKOnly + vbExclamation, _
"Error No: " & Err.Number
Resume Exit_DemoShowAccess

End Sub

Public Function ShowAccess( _
Optional ShowErrorMessage As Boolean = False) As Boolean

' RETURNS:
'
' TRUE if the call to SetForegroundWindow() succeeds;
' otherwise, FALSE.

Dim lngL As Long

' Call API function:
lngL = SetForegroundWindow(Access.Application.hWndAccessApp)

' See if call failed:
If lngL = 0 Then
If ShowErrorMessage Then
MsgBox "The SetForegroundWindow() API function " _
& "returned error number: " & Err.LastDllError, _
vbOKOnly + vbExclamation, "Error Information"
End If
Else
ShowAccess = True
End If

End Function
 

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