Rymare
New Member
- Joined
- Apr 20, 2018
- Messages
- 37
I have some code that does a bunch of things and recently I changed a sheet name. and deleted a column I went through all my code to change the sheet code and the column number and double checked everything. However, now my code won't work. I won't send the email, it gives me an "Error code: 'Run-time error '287'. Application-defined or object-defined error.'" on the .Send line. It works fine when I put .Display, and it was working perfectly before I made my edits. I'll post the current code and the prior code that was working.
NEW CODE (that doesn't work):
OLD CODE (which worked, but is not useful now):
Any help is appreciated!
NEW CODE (that doesn't work):
Code:
Sub mailing()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim lastrow As Long
Dim ws As Worksheet
lastrow = Worksheets("2019").Cells(Rows.Count, "S").End(xlUp).Row
Dim rg As Range
Dim rg2 As Range
Set ws = Worksheets("2019")
With ws
lastrow = .Cells(Rows.Count, "S").End(xlUp).Row
Set rg = Range(.Cells(1, "S"), .Cells(lastrow, "S"))
Set rg2 = Range(.Cells(1, "T"), .Cells(lastrow, "T"))
End With
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
For Each cell In rg2
If cell.Value = "sent" Then
Cells(cell.Row, "S").Value = ""
End If
Next cell
For Each cell In rg
Set OutMail = OutApp.CreateItem(0)
If cell.Value Like "?*@?*.?*" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "S").Value
.Subject = "Work Order: " & Cells(cell.Row, "G").Value & " assigned"
.Body = "Work Order: " & Cells(cell.Row, "G").Value & _
" has been assigned to you." & _
vbNewLine & vbNewLine & _
"Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
"District: " & Cells(cell.Row, "C").Value & vbNewLine & _
"City: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
"Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
.ReadReceiptRequested = True
.display
.Send
End With
Cells(cell.Row, "T").Value = "sent"
Cells(cell.Row, "V").Value = Now
Cells(cell.Row, "U").Value = Cells(cell.Row, "S").Value
Set OutMail = Nothing
End If
Next cell
'Set OutApp = Nothing 'it will be Nothing after End Sub
Application.ScreenUpdating = True
End Sub
OLD CODE (which worked, but is not useful now):
Code:
Sub mailing()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim lastrow As Long
Dim ws As Worksheet
lastrow = Worksheets("2018").Cells(Rows.Count, "T").End(xlUp).Row
Dim rg As Range
Dim rg2 As Range
Set ws = Worksheets("2018")
With ws
lastrow = .Cells(Rows.Count, "T").End(xlUp).Row
Set rg = Range(.Cells(1, "T"), .Cells(lastrow, "T"))
Set rg2 = Range(.Cells(1, "u"), .Cells(lastrow, "u"))
End With
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
For Each cell In rg2
If cell.Value = "sent" Then
Cells(cell.Row, "T").Value = ""
End If
Next cell
For Each cell In rg
Set OutMail = OutApp.CreateItem(0)
If cell.Value Like "?*@?*.?*" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "T").Value
.Subject = "Work Order: " & Cells(cell.Row, "G").Value & " assigned"
.Body = "Work Order: " & Cells(cell.Row, "G").Value & _
" has been assigned to you." & _
vbNewLine & vbNewLine & _
"Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
"District: " & Cells(cell.Row, "C").Value & vbNewLine & _
"City: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
"Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
.ReadReceiptRequested = True
.Send
End With
Cells(cell.Row, "u").Value = "sent"
Cells(cell.Row, "w").Value = Now
Cells(cell.Row, "V").Value = Cells(cell.Row, "t").Value
Set OutMail = Nothing
End If
Next cell
'Set OutApp = Nothing 'it will be Nothing after End Sub
Application.ScreenUpdating = True
End Sub
Any help is appreciated!
Last edited: