Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 128
  • Last Modified:

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
0
Natasha Adams
Asked:
Natasha Adams
  • 5
  • 5
1 Solution
 
Saqib Husain, SyedEngineerCommented:
Try something like

Myitem.body = "Hi " & cell.value & vbcrlf & myitem.body
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
 
Natasha AdamsAuthor Commented:
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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?
0
 
Natasha AdamsAuthor Commented:
Name is on Column A and email is on Column B.  Thanks for the quick reply
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
In that case this should work...
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 " & cell.Value & ","
        With MyItem
            .To = cell.Offset(0, 1).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

0
 
Natasha AdamsAuthor Commented:
Thank you so much Subodh.  You are so smart!  you saved my life.  Thank you.
0
 
Natasha AdamsAuthor Commented:
I don't know what the difference in Assisted and Best Solution?
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
 
Natasha AdamsAuthor Commented:
Subodh was great responded very quickly and helped me resolve my issues.  OMG he is smart and awesome!
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Thanks for the feedback and compliment Natasha!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now