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