GetObject not getting Outlook.Application

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
I have a stock function I use to test to see if Outlook is running before I attempt to draft an email. I've never had problems with it before, but today I ran the program and it doesn't acknowledge that Outlook is running. If I close/reopen the file, it seems to work and if I step through, it'll work as well. I also have a user who states she's having the same problem. I'm running Office 2010.
Code:
Public Function TestOutlookIsOpen(row As Long) As Integer
    Dim oOutlook As Object

    On Error Resume Next
    Set oOutlook = GetObject(, "Outlook.Application")
    On Error GoTo 0

    If oOutlook Is Nothing Then '//The program will occasionally see oOutlook as being empty. 
        TestOutlookIsOpen = -1
        MsgBox "Outlook is not running. Open Outlook and try again."
    Else
    
        If row <> -1 Then
            Call createEmail(row)
            TestOutlookIsOpen = 1 '//Some of my code first tests if Outlook is open and then performs tasks after, the rest do it up front and then tests.
        End If
    End If
End Function
 
Last edited by a moderator:
I've try your excel file.
If I run any form and click the submit button, excel keep crashing.

Import everything to new blank. Cleaning and fixing createEmail and change TestOutlookIfOpen using new approach.
I'm able to send around 50 emails to my mail server using loop and data changes without crashing. Saving also no problem.
I'm using excel 2007 and outlook 2010.
Is it ok if put your modified createEmail function here?

Code:
[COLOR=#d3d3d3]'
'based on code by ZVI - MrExcelMVP
'mod by lhartono[/COLOR]
[COLOR=#0000cd]Public Function TestOutlook()
    Dim oScript         As Object
    Dim OutlookPath     As String
    Const strAppID = "Outlook.Application"
    
    On Error Resume Next
        
        Set TestOutlook = GetObject(, strAppID)
        
        If Err Then
            Err.Clear
            On Error GoTo HELL
            Set oScript = CreateObject("Wscript.Shell")
            OutlookPath = oScript.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" & "OUTLOOK.EXE" & "\")
            Set oScript = Nothing
            Call VBA.Shell(OutlookPath, vbNormalNoFocus)
            Set TestOutlook = GetObject(, strAppID)
        End If
GETOUT:
        Exit Function
HELL:
        Dim errString As String
        Select Case Err.Number
            Case 53
                            errString = "File Not Found: " & OutlookPath
            Case 429
                            errString = "Outlook initialization failed..."
            Case -2147024894
                            errString = "Outlook is not registered in this system."
            Case Else
                            errString = Err.Description
        End Select
        MsgBox "Error #  " & Err.Number & vbNewLine & vbNewLine & errString & vbNewLine & vbNewLine & "Source :  " & Err.Source, vbCritical
        Resume GETOUT
End Function[/COLOR]
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Sure; the CreateEmail function was found online (I can never remember where) about a year ago and has been modified to fit my needs.

Code:
Public Function createEmail(row As Long)
                        
    Dim wb1             As Workbook
    Dim wb2             As Workbook
    Dim newWS           As Worksheet
    Dim ws              As Worksheet
    Dim TempFilePath    As String
    Dim TempFileName    As String
    Dim FileExtStr      As String
    Dim fRow            As Integer
    Dim lRow            As Integer
    Dim i, j            As Integer
    Dim BodyTxt         As String
    Dim SendTo          As String
    Dim OutApp          As Object
    Dim NewMail         As Object
    Dim writeA          As Boolean
    Dim Answer          As Integer
    Dim cReq            As Variant
    Dim clipboard      As MSForms.DataObject

    Set clipboard = New MSForms.DataObject

    ThisWorkbook.Unprotect ""
    
    Set wb1 = ThisWorkbook
    
    If row = 1 Then
        Set ws = ThisWorkbook.Worksheets("Positions")
    ElseIf row = 2 Then
        Set ws = ThisWorkbook.Worksheets("Updates Needed")
    End If
    
    Set otlApp = CreateObject("Outlook.Application")
    Set otlNewMail = otlApp.CreateItem(olMailItem)
    
    fRow = FirstInst 'FirstInst and LastInst are global variables
    lRow = LastInst
        
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    

    ' Make a copy of the file.
    ' If you want to change the file name then change only TempFileName variable.
    TempFilePath = Environ$("temp") & "\"
    If row = 1 Then
    
        TempFileName = UserInst & " Update" 'UserInst is a global variable
    Else
        TempFileName = "Updates Required"
    End If
    
    FileExtStr = ".xlsx"

    Set wb2 = Workbooks.Add
    wb2.SaveAs TempFilePath & TempFileName & FileExtStr
    
    If row = 1 Then
        ws.Rows(1).EntireRow.Copy
        wb2.Worksheets("Sheet1").Range("A1").Select
        wb2.Worksheets("Sheet1").Paste
    
        wb2.Worksheets("Sheet1").Name = TempFileName
    
        j = 2
        
        For i = fRow To lRow
            ws.Rows(i).EntireRow.Copy
            wb2.Worksheets(TempFileName).Rows(j).Select
            wb2.Worksheets(TempFileName).Paste
            j = j + 1
        Next i
            'ws.Cells(1, 1).Select
            wb2.Worksheets(TempFileName).Cells(1, 1).Select
        
        BodyTxt = "Attention Staff," & vbNewLine & vbNewLine & _
                   "Please be advised that this is an automated message created by one of your staff members using the Automated Library Contact List Update Program." & vbNewLine & vbNewLine & _
                    UserName & " has submitted the attached report. Please retain this information." & vbNewLine & vbNewLine & _
                    "Kind Regards," & vbNewLine & vbNewLine & "Library Report Bot"
    ElseIf row = 2 Then
        ws.Activate
        ws.Range("A1", "L174").Select
        Selection.Copy
        Set newWS = wb2.Worksheets("Sheet1")
        newWS.Activate
        newWS.Cells.Select
        newWS.Paste
        newWS.Cells(1, 1).Select
        
        
        BodyTxt = "Attention Staff," & vbNewLine & vbNewLine & _
                   "Please be advised that this is an automated message created by one of your staff members using the Automated Library Contact List Update Program." & vbNewLine & vbNewLine & _
                   "The following sheet contains a list of users whose contact list has not been updated during this quarter." & _
                   " You may wish to reach out the Senior Librarian at each institution to update their respective contacts." & vbNewLine & vbNewLine & _
                   "Kind Regards," & vbNewLine & vbNewLine & "Library Report Bot"
    End If
    
    clipboard.Clear
    Application.CutCopyMode = False
    wb2.Application.CutCopyMode = False
    
    
    
    SendTo = ""
     
    wb2.Save
      
    Set OutApp = CreateObject("Outlook.Application")
    
    Set NewMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With NewMail
        .To = SendTo
        .CC = ""
        .BCC = ""
        .Subject = UserInst & " Contact List Update " & Format(Now, "dd-mmm-yy h-mm-ss")
        .Body = BodyTxt
        .Attachments.Add wb2.FullName
        .Display
    End With
    
    wb2.Close savechanges:=False
    
    On Error GoTo 0
    
    MsgBox "The program completed this task successfully."
    
    Kill TempFilePath & TempFileName & FileExtStr
    Set NewMail = Nothing
    Set otlApp = Nothing
     With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
   
ExitSub:
End Function
 
Last edited:
Upvote 0
Actually it is minor changes.

Code:
[COLOR=#0000cd]Public Function createEmail(ByVal paramRow As Long)
    Dim OutApp          As Object       'Outlook.Application
    Dim NewMail         As Object
    
    Dim wb1             As Workbook
    Dim wb2             As Workbook
    Dim newWS           As Worksheet
    Dim ws              As Worksheet
    Dim TempFilePath    As String
    Dim TempFileName    As String
    Dim FileExtStr      As String
    Dim fRow            As Integer
    Dim lRow            As Integer
    Dim i, j            As Integer
    Dim BodyTxt         As String
    Dim SendTo          As String
    Dim writeA          As Boolean
    Dim Answer          As Integer
    Dim cReq            As Variant
    Dim strWB2          As String

[B]    Set OutApp = TestOutlook()             [/B][/COLOR][COLOR=#d3d3d3]' do this first before processing worksheet if the goal is to send email.[/COLOR][COLOR=#0000cd][B]
    If OutApp Is Nothing Then
        Exit Function
    End If[/B]

    Set wb1 = ThisWorkbook
    wb1.Unprotect ""
    If paramRow = 1 Then
        Set ws = wb1.Worksheets("Positions")
    ElseIf paramRow = 2 Then
        Set ws = wb1.Worksheets("Updates Needed")
    End If
    
    fRow = FirstInst
    lRow = LastInst
    ' Make a copy of the file.
    ' If you want to change the file name then change only TempFileName variable.
    TempFilePath = Environ$("temp") & "\"
    If paramRow = 1 Then
        TempFileName = UserInst & " Update"
    Else
        TempFileName = "Updates Required"
    End If
    FileExtStr = ".xlsx"
    
    With Application
        .ScreenUpdating = False
        '.EnableEvents = False
    End With

    Set wb2 = Workbooks.Add
    
    If paramRow = 1 Then
        ws.Rows(1).EntireRow.Copy
        wb2.Worksheets("Sheet1").Range("A1").Select
        wb2.Worksheets("Sheet1").Paste
        Application.CutCopyMode = False
        wb2.Worksheets("Sheet1").Name = TempFileName
    
        j = 2
        
        For i = fRow To lRow
            ws.Rows(i).EntireRow.Copy
            wb2.Worksheets(TempFileName).Rows(j).Select
            wb2.Worksheets(TempFileName).Paste
            Application.CutCopyMode = False
            j = j + 1
        Next i
            'ws.Cells(1, 1).Select
        wb2.Worksheets(TempFileName).Cells(1, 1).Select
        
        BodyTxt = "Attention Staff," & vbNewLine & vbNewLine & _
                  "Please be advised that this is an automated message created by one of your staff members using the Automated Library Contact List Update Program." & vbNewLine & vbNewLine & _
                   UserName & " has submitted the attached report. Please retain this information." & vbNewLine & vbNewLine & _
                  "Kind Regards," & vbNewLine & vbNewLine & "Library Report Bot"
    ElseIf paramRow = 2 Then
        ws.Activate
        ws.Range("A1", "L174").Select
        Selection.Copy
        
        Set newWS = wb2.Worksheets("Sheet1")
        newWS.Activate
        newWS.Cells.Select
        newWS.Paste
        Application.CutCopyMode = False
        newWS.Cells(1, 1).Select
        
        BodyTxt = "Attention Staff," & vbNewLine & vbNewLine & _
                  "Please be advised that this is an automated message created by one of your staff members using the Automated Library Contact List Update Program." & vbNewLine & vbNewLine & _
                  "The following sheet contains a list of users whose contact list has not been updated during this quarter." & _
                  " You may wish to reach out the Senior Librarian at each institution to update their respective contacts." & vbNewLine & vbNewLine & _
                  "Kind Regards," & vbNewLine & vbNewLine & "Library Report Bot"
    End If
    
    Application.CutCopyMode = False

    wb2.SaveAs TempFilePath & TempFileName & FileExtStr
    strWB2 = wb2.FullName
    wb2.Close savechanges:=False
    
    With Application
        .ScreenUpdating = True
        '.EnableEvents = True
    End With

[B]    SendTo = "me@work.com"[/B]
    
[B]    Set OutApp = TestOutlook()
    
    If Not OutApp Is Nothing Then
        Set NewMail = OutApp.CreateItem(0)
        With NewMail
            .To = SendTo
            .CC = ""
            .BCC = ""
            .Subject = UserInst & " Contact List Update " & Format(Now, "dd-mmm-yy h-mm-ss")
            .Body = BodyTxt
            .Attachments.Add (strWB2)
            '.Send
            .Display
        End With
   
        MsgBox "The program completed this task successfully."
    
        Kill TempFilePath & TempFileName & FileExtStr
        Set NewMail = Nothing
        Set OutApp = Nothing
    End If[/B]
    
    Set ws = Nothing
    Set newWS = Nothing
    Set wb2 = Nothing
    Set wb1 = Nothing

ExitSub:
End Function
[/COLOR]
 
Upvote 0
I thought that might be the case. I have not seen the image error message but I have seen the clipboard message to save it or not. There are 2 ways to avoid that.

The first is most appropriate here I think:
1. After the Paste, Application.CutCopyMode = False


I am totally Agreed.
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,493
Members
450,016
Latest member
murarj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top