Natasha Adams
asked on
How to add contact name to VBA code to automate emails in outlook using a Template
How to add contact name from the excel column A to the template as Hi Adam to this existing code?
Sub SendEmail()
Dim rng As Range
Dim cell As Range
Dim myOlApp As Object
Dim MyItem As Object
'Specifies number of rows to search for email addresses
Set rng = Range("A1:A5")
Set myOlApp = CreateObject("Outlook.Appl ication")
For Each cell In rng.Cells
Set MyItem = myOlApp.CreateItemFromTemp late("C:\U sers\.oft" )
With MyItem
.To = cell.Value
.Subject = "Help"
.Send
End With
Set MyItem = Nothing
Next
myOlApp.Quit
Set myOlApp = Nothing
End Sub
Sub SendEmail()
Dim rng As Range
Dim cell As Range
Dim myOlApp As Object
Dim MyItem As Object
'Specifies number of rows to search for email addresses
Set rng = Range("A1:A5")
Set myOlApp = CreateObject("Outlook.Appl
For Each cell In rng.Cells
Set MyItem = myOlApp.CreateItemFromTemp
With MyItem
.To = cell.Value
.Subject = "Help"
.Send
End With
Set MyItem = Nothing
Next
myOlApp.Quit
Set myOlApp = Nothing
End Sub
You may use something like this....
Sub SendEmail()
Dim rng As Range
Dim cell As Range
Dim myOlApp As Object
Dim MyItem As Object
Dim strBody As String
'Specifies number of rows to search for email addresses
Set rng = Range("A1:A5")
Set myOlApp = CreateObject("Outlook.Application")
For Each cell In rng.Cells
Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\.oft")
strBody = "Hi " & Left(cell.Value, InStr(cell.Value, "@") - 1) & ","
With MyItem
.To = cell.Value
.Subject = "Help"
.Body = strBody & vbNewLine & .Body
.Display
'.Send
End With
Set MyItem = Nothing
Next
myOlApp.Quit
Set myOlApp = Nothing
End Sub
ASKER
Thank you Subodh and Syed for your contributions. it does say Hi and the email address. I want it to say Hi John where the name is listed in Column A? How could I change it to reflect Name in Column A?
Thanks
Thanks
As per your code .To = cell.Value, the email addresses are supposed to be in col. A.
Would you please let us know, in which columns, Names and their email addresses are listed?
Would you please let us know, in which columns, Names and their email addresses are listed?
ASKER
Name is on Column A and email is on Column B. Thanks for the quick reply
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much Subodh. You are so smart! you saved my life. Thank you.
ASKER
I don't know what the difference in Assisted and Best Solution?
You're welcome Natasha! Glad to help.
Best solution is one which completely resolved your question and the assisted solution is one which also addressed your question correctly and partly helped you to resolve the issue.
Best solution is one which completely resolved your question and the assisted solution is one which also addressed your question correctly and partly helped you to resolve the issue.
ASKER
Subodh was great responded very quickly and helped me resolve my issues. OMG he is smart and awesome!
Thanks for the feedback and compliment Natasha!
Myitem.body = "Hi " & cell.value & vbcrlf & myitem.body