Runtime error 424 'Object required' sending VBA emails

Hoareman

New Member
Joined
Jun 3, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hi,

I have been trying to wrist a code to send emails via excel.

All the information I need is contained within the a worksheet, including email address and email body. Email addresses are contained in Column E.
Each row contains slightly different information.

Here is the code:

Sub EmailBody()

Dim SendTo As String
Dim MailBody As String

For Each i In Range("A2:A100")

MailBody = "Dear " & i.Offset(0, 5) & "," & vbCr & vbCr & i.Offset(0, 6) & vbCr & vbCr & i.Offset(0, 7) & vbCr & vbCr & i.Offset(0, 8) & vbCr & vbCr & "Hospital Number: " & i.Value & vbCr & "Exam Date: " & i.Offset(0, 1) & vbCr & "Exam: " & i.Offset(0, 2) & vbCr & vbCr & "Kind regards," & vbCr & vbCr & i.Offset(0, 11) & vbCr & i.Offset(0, 9) & vbCr & i.Offset(0, 10)

SendTo = i.Offset(0, 5).Value

MsgBox MailBody & vbCr & vbCr & SendTo

With outMail
.To = SendTo
.Subject = "Images for QA"
.Body = MailBody
.Display
End With

Next i

End Sub

My problem here is two-fold.

1. I am getting runtime error at the .To = SendTo line (highlighted in yellow)
2. The code was originally more complicated so I have tried to simplify it. My previous problem was that the code was creating a new email for every address in column E, but was only using data from the top row of the range.

Any help would be greatly appreciated as I cannot figure it out.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Your Offsets seem to be off and you didn't declare outMail, you'll probably need to adjust more offsets.

VBA Code:
Sub EmailBody()

Dim SendTo As String
Dim MailBody As String
Dim objOutlook As Object
Dim outMail As Object

Set objOutlook = CreateObject("Outlook.Application")

For Each i In Range("A2:A100")

MailBody = "Dear " & i.Offset(0, 5) & "," & vbCr & vbCr & i.Offset(0, 6) & vbCr & vbCr & i.Offset(0, 7) & vbCr & vbCr & i.Offset(0, 8) & vbCr & vbCr & "Hospital Number: " & i.Value & vbCr & "Exam Date: " & i.Offset(0, 1) & vbCr & "Exam: " & i.Offset(0, 2) & vbCr & vbCr & "Kind regards," & vbCr & vbCr & i.Offset(0, 11) & vbCr & i.Offset(0, 9) & vbCr & i.Offset(0, 10)

SendTo = i.Offset(0, 4).Value

MsgBox MailBody & vbCr & vbCr & SendTo

Set outMail = objOutlook.CreateItem(olMailItem)

With outMail
.To = SendTo
.Subject = "Images for QA"
.Body = MailBody
.Display
End With

Set outMail = Nothing

Next i

Set objOutlook = Nothing

End Sub
 
Upvote 0
Thanks mrshl9898

That code worked brilliantly.

Could I add another task to your code? There will not always be data in all the cell in column A. At the moment the code runs, creating blank emails with no addresses.
Is there a code that can be added so once it hits an empty cell in column A, the code stops creating emails.

Thanks
 
Upvote 0
This should do it (untested)

VBA Code:
VBA Code:
Sub EmailBody()

Dim SendTo As String
Dim MailBody As String
Dim objOutlook As Object
Dim outMail As Object

Set objOutlook = CreateObject("Outlook.Application")

For Each i In Range("A2:A100")

If i.Value = "" then
Exit Sub
End If

MailBody = "Dear " & i.Offset(0, 5) & "," & vbCr & vbCr & i.Offset(0, 6) & vbCr & vbCr & i.Offset(0, 7) & vbCr & vbCr & i.Offset(0, 8) & vbCr & vbCr & "Hospital Number: " & i.Value & vbCr & "Exam Date: " & i.Offset(0, 1) & vbCr & "Exam: " & i.Offset(0, 2) & vbCr & vbCr & "Kind regards," & vbCr & vbCr & i.Offset(0, 11) & vbCr & i.Offset(0, 9) & vbCr & i.Offset(0, 10)

SendTo = i.Offset(0, 4).Value

MsgBox MailBody & vbCr & vbCr & SendTo

Set outMail = objOutlook.CreateItem(olMailItem)

With outMail
.To = SendTo
.Subject = "Images for QA"
.Body = MailBody
.Display
End With

Set outMail = Nothing

Next i

Set objOutlook = Nothing
 
Upvote 0
Solution

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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