Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to add contact name to VBA code to automate emails in outlook using a Template

Posted on 2016-09-10
11
Medium Priority
?
110 Views
Last Modified: 2016-09-11
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
Comment
Question by:Natasha Adams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41793131
Try something like

Myitem.body = "Hi " & cell.value & vbcrlf & myitem.body
0
 
LVL 32

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41793173
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
 

Author Comment

by:Natasha Adams
ID: 41793418
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41793423
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
 

Author Comment

by:Natasha Adams
ID: 41793439
Name is on Column A and email is on Column B.  Thanks for the quick reply
0
 
LVL 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 total points
ID: 41793444
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
 

Author Comment

by:Natasha Adams
ID: 41793502
Thank you so much Subodh.  You are so smart!  you saved my life.  Thank you.
0
 

Author Comment

by:Natasha Adams
ID: 41793503
I don't know what the difference in Assisted and Best Solution?
0
 
LVL 32

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41793508
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
 

Author Closing Comment

by:Natasha Adams
ID: 41793525
Subodh was great responded very quickly and helped me resolve my issues.  OMG he is smart and awesome!
0
 
LVL 32

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41793710
Thanks for the feedback and compliment Natasha!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question