Joe Boatman
New member
- Local time
- Today, 13:44
- Joined
- May 30, 2020
- Messages
- 25
This is an addition to modOutlook and shows a function to return Outlook as an Object whether or not Outlook is open or not.
Code:
'Globals
Public gnErr As Long 'Last err number
Public gsErr As String 'Last error message
Public gsPrompt As String 'Msgbox prompt
'15 Mar 2004
Public Function apGetOutlook(Optional ByRef bAlreadyRunning As Boolean) As Object
'Open Outlook to ensure any error message are seen and do not hang PC because they are hidden
'Return reference to Outlook object
'20 Feb 2007: Returns if Outlook already running
Const olFolderInbox As Long = 6, olMinimized As Long = 1
' Dim olApp As Outlook.Application
Dim olApp As Object
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application") 'Err if not open
If Err <> 0 Then 'Not open, so open
gnErr = Err.Number: gsErr = Err.Description
Err.Clear
Set olApp = CreateObject("Outlook.Application") 'Opens OL hidden
If Err = 0 Then
With olApp.Explorers
.Add olApp.Session.GetDefaultFolder(olFolderInbox)
.Item(1).Activate
.Item(1).WindowState = olMinimized
End With
bAlreadyRunning = True
Else '12 Nov 2006
gnErr = Err.Number: gsErr = Err.Description
gsPrompt = "MS Outlook does not appear to be available on this computer."
VBA.MsgBox gsPrompt, vbInformation, "Cannot find Microsoft Outlook application"
bAlreadyRunning = False
End If
Else
bAlreadyRunning = True
End If
On Error GoTo 0
Set apGetOutlook = olApp
End Function
Last edited: