VBA Won't Send Outlook Email

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):

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:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
? ".display"
Did the VBE not capitalize that? Should have.
 
Upvote 0
I really need an answer, and I already tried adjusting my security protocols and I enabled all the appropriate setting in excel VBA references.

So, I'm going to *bump this
 
Upvote 0
.
This works here :

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, "T").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
                .Send
                '.Display
            End With
            Cells(cell.Row, "T").Value = "sent"
            Cells(cell.Row, "U").Value = Now
            Cells(cell.Row, "V").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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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