Link to home
Start Free TrialLog in
Avatar of Natasha Adams
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.Application")
     
    For Each cell In rng.Cells
        Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\.oft")
        With MyItem
            .To = cell.Value
            .Subject = "Help"
            .Send
        End With
        Set MyItem = Nothing
    Next
   
    myOlApp.Quit
    Set myOlApp = Nothing

End Sub
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

Try something like

Myitem.body = "Hi " & cell.value & vbcrlf & myitem.body
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

Open in new window

Avatar of Natasha Adams
Natasha Adams

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
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?
Name is on Column A and email is on Column B.  Thanks for the quick reply
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much Subodh.  You are so smart!  you saved my life.  Thank you.
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.
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!