VBA - help to make e-mailadresse based on a specific cellvalue.

FredrikUhre

New Member
Joined
Dec 13, 2024
Messages
2
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hello
I have created a code so the subject and body of the e-mail is based on a specific cell. Can anybody tell how to make the string for the recipient, based on a specifik cell.

My code is down below, i hobe somebody can help me with this problem.

VBA Code:
Sub SendEmail()
    Dim objOutlook As Object
    Dim objMail As Object
    Dim strTo As String
    Dim strSubject As String
    Dim strBody As String
    'Set email properties
    strTo = Sheets("Ark1").Cells(4, 2) 
    strSubject = Sheets("Ark1").Cells(2, 2)
    strBody = Sheets("Ark1").Cells(1, 2)
    'Create Outlook object
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)
    'Set email recipients, subject, and body
    With objMail
        .To = strTo
        .Subject = strSubject
        .Body = strBody
        .Send
    End With
    'Clean up
    Set objMail = Nothing
    Set objOutlook = Nothing
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
But you already have in the code:
VBA Code:
    strTo = Sheets("Ark1").Cells(4, 2)

so string strTo is a value from cell B4 from sheet Ark1

and then use it in
objMail.To

may be in cell B4 you dont't have a valid e-mail address?
 
Upvote 0
Solution
Gald to hear that*. And thanks for marking my post as a solution.

*) of course I'm glad because you noted this, not because you misplaced columns and rows. Almost (?) everybody programming in VBA did that (I did many times), so John 8:7 applies :-)
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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