To get the Outlook object whether Outlook is open or closed, I use code like the following:
Option Explicit
'SetForegroundWindow() is used by MoveFocusToWorksheet()
#If VBA7 And Win64 Then
'64 bit Excel:
'All of the Win64 lines are supposed to be RED in 32 bit Excel
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
'32 bit Excel:
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#End If
'Outlook enumeration constant reference: https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
Private Const olMinimized As Long = 1
Private Const olMaximized As Long = 2
Private Const olFolderInbox As Long = 6
Sub TestGetOutlookObject()
Dim OutApp As Object
'Get the Outlook Object if Outlook is already open
On Error Resume Next
Set OutApp = LjmOpenOutlook()
'Exit if the Outlook Object could not be created
If OutApp Is Nothing Then
MsgBox "NOTHING DONE. Outlook could not be opened."
GoTo MYEXIT
End If
Call MoveFocusToWorksheet(ActiveSheet)
MsgBox "Outlook Object was successfully obtained."
MYEXIT:
'Clear object pointers
Set OutApp = Nothing
End Sub
Function LjmOpenOutlook( _
Optional iWindowState As Long = olMinimized, _
Optional bReleaseIt As Boolean = False _
) As Object
'This opens outlook if it is NOT already open
'Reference: http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'Thank you Ron deBruin and Ben Clothier
'
'Late Binding is used in the following routines
Static myStaticOutlookObject As Object
On Error GoTo ERROR_HANDLER
Select Case True
Case myStaticOutlookObject Is Nothing, Len(myStaticOutlookObject.Name) = 0
Set myStaticOutlookObject = GetObject(, "Outlook.Application")
If myStaticOutlookObject.Explorers.Count = 0 Then
INIT_OUTLOOK:
'Open the Outlook inbox to prevent errors with security prompts
'Set the requested Outlook Windows state (i.e. Minimized, Maximized)
myStaticOutlookObject.Session.GetDefaultFolder(olFolderInbox).display
myStaticOutlookObject.ActiveExplorer.WindowState = iWindowState
End If
Case bReleaseIt
'Clear the STATIC Outlook Object if requested by the input option
Set myStaticOutlookObject = Nothing
End Select
'Set the return value
Set LjmOpenOutlook = myStaticOutlookObject
GoTo MYEXIT
ERROR_HANDLER:
Select Case Err.Number
Case -2147352567
'User cancelled setup, silently exit
Set myStaticOutlookObject = Nothing
Case 429, 462
'Error 462 - Outlook is NOT open
Set myStaticOutlookObject = GetOutlookApp()
If myStaticOutlookObject Is Nothing Then
Err.Raise 429, "LjmOpenOutlook", "Outlook Application does not appear to be installed."
Else
Resume INIT_OUTLOOK
End If
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error attempting to OPen Outlook."
End Select
MYEXIT:
Exit Function
End Function
Private Function GetOutlookApp() As Object
'This returns a pointer to the Outlook Object if Outlook is available
'
'This function is used exclusively by LjmOpenOutlook() and is part of the
'original Ron deBruin code referenced above
Dim myOutlookObject As Object
'Attempt to create an Outlook object
On Error Resume Next
Set myOutlookObject = CreateObject("Outlook.Application")
On Error GoTo 0
'Clear any error that occurs and also clear the object
If Err.Number <> 0 Then
Err.Clear
Set myOutlookObject = Nothing
End If
'Set the return value
Set GetOutlookApp = myOutlookObject
'Clear the local object pointer
Set myOutlookObject = Nothing
End Function
Sub MoveFocusToWorksheet(ws As Worksheet)
'This moves the focus from another application (e.g. Outlook) back to Excel
'
'Reference: http://stackoverflow.com/questions/28042521/set-focus-back-to-the-application-window-after-showing-userform
'Thank you Gene Skuratovsky
'Put the focus on the Workbook and Worksheet from the input parameter
ws.Parent.Activate
ws.Activate
'Move the above Workbook/Worksheet to the front
Call SetForegroundWindow(Application.hwnd)
End Sub
It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
This option requires all variables to be declared and will give a compiler error for undeclared variables.
Lewis
Bookmarks