Solved

VBA code to automate emails in outlook using a Template

Posted on 2016-09-06
19
53 Views
Last Modified: 2016-09-28
I am getting an error at .Send can you please let me know what is wrong?  Thanks in advance

Sub SendEmail()


Dim rng As Range
Dim row As Range
Dim cell As Range
Dim from As String
Dim template As String

'Specifies number of rows to search for email addresses
Set rng = Range("A1:A5")

Dim myOlApp As Object
Dim MyItem As Object

For Each row In rng.Rows
    For Each cell In row.Cells
   
        Set myOlApp = CreateObject("Outlook.Application")
        Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\.oft")
       
        With MyItem
                    .To = cell.Value
                    .Subject = "Help"
                    .Send
                End With
           
            Set MyItem = Nothing
            Set myOlApp = Nothing
       
    Next cell
    Next row

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
  • 9
  • 7
  • 2
  • +1
19 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41787223
Is the Template filename correct? Also verify that the Cell.Value is correct and recognized by Outlook as a valid name/address.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41787251
what's the error message you getting here?
0
 

Author Comment

by:Natasha Adams
ID: 41787717
I copied the code from the internet so not sure what should go to .cell value.  It stops at .Send
please help I have created a template in .oft and have an excel spreadsheet with email address's to send the template to all on the emails.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41788758
The code you have loops through cells A1:A5 of your worksheet and uses the values in those cells for  the email address. If any of the values in cells A1:A5 is blank or an invalid address, Outlook will give you an error when you try to Send it.

Secondly, the path to the template needs to the actual template file, not just the folder. What is the actual name of the template?
0
 

Author Comment

by:Natasha Adams
ID: 41788991
it is still giving an error at .send  the template file is correct.  Can you please help as I have to send these emails tomorrow.
0
 

Author Comment

by:Natasha Adams
ID: 41788992
is the code that is copied right?
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points (awarded by participants)
ID: 41789000
What error did you get?

Otherwise the code looks OK. I would remove one of the For/Next loops though and just loop through the cells in the specified range. Also, there's no need to recreate the Outlook object for each email. Do it once and use that for each email, then quit at the end.

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

Open in new window

0
 

Author Comment

by:Natasha Adams
ID: 41789005
Thank you.  Let me try it now.
0
 

Author Comment

by:Natasha Adams
ID: 41789007
Wayne I copied your code and still getting an error at .send   What could be the reason?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41789020
I've tested the code here (although without a template as I currently only have access to Outlook 2007), and it works fine UNLESS a value in cells A1:A5 is not a valid name or email address.

Otherwise I don't know if you don't tell me what error you are getting.
0
 
LVL 31

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 250 total points (awarded by participants)
ID: 41789112
To make sure all the fields are being properly filled in generated emails, Replace .Send with .Display in the Wayne's proposed code and if you are sure that all the fields are filled properly, try to send one of the generated emails manually.
If you cannot send a correct email manually that means outlook is not configured properly.
0
 

Author Comment

by:Natasha Adams
ID: 41789401
Wayne and Subodh it worked Thank you.  I put .Display and it worked then I went back and put .send and it worked.  OMG thank you so much.  I really appreciate your help.
0
 

Author Comment

by:Natasha Adams
ID: 41789404
Where could I learn VBA online can you recommend some resources.
0
 
LVL 31

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41789440
You're welcome Natasha! Glad we could help.
There is plenty of stuff available online, just Google "Excel VBA tutorials for beginner", you can search for the same on YouTube as well.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41790511
Natasha,

I'm glad it's finally worked for you. Next time please don't forget to include the actual error message you received. We might have been able to solve this almost immediately if you had.

Wayne
0
 

Author Comment

by:Natasha Adams
ID: 41790658
Thank you Wayne and Subodh I will next time.  How would I add to the existing code Hi and to pick up the persons name from excel and include?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41790669
Natasha, that might be best asked in a separate question.
0
 

Author Comment

by:Natasha Adams
ID: 41792057
ok
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41819424
Both comments lead to the solution working.
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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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