Outlook VBA routines Part 3 - the support functions (1 Viewer)

Joe Boatman

New member
Local time
Today, 07:52
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:

Isaac

Lifelong Learner
Local time
Yesterday, 23:52
Joined
Mar 14, 2017
Messages
3,294
There are undeclared variables in here...
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:52
Joined
Sep 21, 2011
Messages
7,500
There are undeclared variables in here...
I think they are globals from the name.?

However I used to do something similar with a function I found, but then I found it did not matter whether it was running or not.? :confused:

Code:
    ' See if Outlook is open, otherwise open it
    'If fIsOutlookRunning = False Then
    Set objOutlook = CreateObject("Outlook.Application")
    'Call OpenOutlook
    'Pause (5)
    ' Else
    'Set objOutlook = GetObject(, "Outlook.Application")
    'End If
 

Isaac

Lifelong Learner
Local time
Yesterday, 23:52
Joined
Mar 14, 2017
Messages
3,294
I think they are globals from the name.?
Ahh.
However I used to do something similar with a function I found, but then I found it did not matter whether it was running or not.?
Yeah I have had varying success with GetObject on both Excel and Outlook and kind of stopped using it. Not really sure why. Maybe to do with a specific computer, most likely.
 

Users who are viewing this thread

Top Bottom