Sending an Email by referencing another sheet

ChaosPup

New Member
Joined
Sep 27, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have the code below working to send an email from a workbook (ignore the garbage address obviously) -

VBA Code:
Sub Mail_small_Text_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Hello" & vbNewLine & vbNewLine & _
          "text1." & vbNewLine & vbNewLine & _
          "text2"

On Error Resume Next
With OutMail
    .To = "emailaddress@emailaddress.com"
    .CC = ""
    .BCC = ""
    .Subject = "Subject Line"
    .Body = strbody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    .Send   'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

What I want to do instead of manually inserting an email address after .To is for the macro to look at the highlighted cell and reference it to the email address which will be listed in a separate sheet. For example, if sheet 1 has the initials AM in the highlighted cell, I want it to find the address on sheet 2 which matches the initials AM. Anyone have any ideas? Thanks!
 
This worked perfectly. The only other question I have is - one of the addresses it goes to will be fixed, the other will be defined by the code you gave me. Is it possible to send them both on the .To command? I tried it in various formats but couldn't get it to work. I was hoping to copy the email to more than 1 fixed address as well.
You change your .To-line like this.

1 fix address:
VBA Code:
.To = "abc@email.com;" & emailValue

Multiple fixed addresses:
VBA Code:
.To = "abc@email.com;def@email.com" & emailValue
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Mistake on the multiple fixed, you need a semicolon after each address, sorry:

VBA Code:
.To = "abc@email.com;def@email.com;" & emailValue
 
Upvote 0
Solution

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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