First Email Sends, but Second Email Fails during Loop

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
The underlying code should loop through all worksheets in a workbook, exclude certain ones from any action; then filter for overdue items and send an email to the Owner. The code works fine for the first email, but I get an "object variable or IF block not set" error when the code cycles again. I'm not seeing what needs to be changed.

VBA Code:
Sub SendIssueEmails()

Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim m As Workbook
Dim w As Worksheet
Dim i As Integer, WSCount As Integer
Dim mWLR As Long
Dim rng As Range
Dim Recip As String, strBody As String

Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Set m = ThisWorkbook

WSCount = m.Worksheets.Count

strBody = "The subject initiative has at least 1 overdue action item.  Please review the action item(s) and respond with justification for its delinquency." & _
           vbNewLine & "Additionally, ensure you update the initiative tracker."

For Each w In Worksheets
    If w.Name <> "Summaries" And w.Name <> "Inventory" And w.Name <> "Template" And w.Name <> "Variables" Then
        If w.AutoFilterMode = True Then w.AutoFilterMode = False
        w.Range("J11").AutoFilter Field:=10, Criteria1:="Overdue"
        mWLR = w.Range("J" & Rows.Count).End(xlUp).Row
        If w.Range("J" & mWLR).Value = "Overdue" Then
            Set rng = w.Range("A11:K" & mWLR).SpecialCells(xlCellTypeVisible)
            If w.Range("B2") = "RB" Then
                Recip = "RB@Company.com"
            ElseIf w.Range("B2") = "MM" Then
                'Recip = "MM@Company.com"
                Recip = "RB@Company.com"
            End If
            With OutlookMail
                .To = Recip 'Here is where I'm getting the object variable or IF block not set error
                .CC = "RB@Company.com"
                .Subject = w.Range("B4").Value
                .HTMLBody = strBody & "<br>" & _
                            RangetoHTML(rng)
                '.Display
                .Send
            End With
            Set OutlookMail = Nothing
            Set OutlookApp = Nothing
        End If
    End If
Next w
            
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I think it’s because of:
Set OutlookMail = Nothing
Set OutlookApp = Nothing

you’ll need to redefine OutlookMail and OutlookApp when you run the second (and further) loop.
 
Upvote 0
I think it’s because of:
Set OutlookMail = Nothing
Set OutlookApp = Nothing

you’ll need to redefine OutlookMail and OutlookApp when you run the second (and further) loop.
@petertenthije Thanks for the reply! It actually wasn't this section
VBA Code:
            Set OutlookMail = Nothing
            Set OutlookApp = Nothing

But your reply did prompt me to try setting both the OutlookMail and OutlookApp inside of the loop; so thank you for that!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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