Solved

Create loop to create emails based off of a list in Excel

Posted on 2014-04-25
6
253 Views
Last Modified: 2014-04-30
Hello,

I would like to create an email for each row in a spreadsheet which does not have a date in the D column (example spreadsheet attached) and I would like it to ignore each row where there is already a date.

Where there is a date I would like to create an email (I have already been thinking about using the code like something  below





Sub Outlook_email_test()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"

    On Error Resume Next
    With OutMail
        .To = "test@test.com"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = strbody
        '.Send
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


But I would like to put the email address in column C in the .to and the first name in the text somewhere in the strbody  which is in column A. It would also be great to put the current date in column D if an email was generated for that row?

would someone be able to help me create the loop and link this to the email and pull that information needed into the email ;)?

Regards,

GISVPN
Email---Dates-based.xlsx
0
Comment
Question by:gisvpn
  • 4
  • 2
6 Comments
 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 500 total points
ID: 40022848
Have you looked at MS Word mail merge using e-mail option?

That would do this for you without any fancy VBA script. When setting up the merge there is an option to filter certain records on fields within the data so that would get over the date requirement.

However, this wouldn't then add the date for those sent but applying the same filter to the data set would then show blank cells to be populated with simple one off entry in one cell and then fill for remainder that are visible.

Thanks
Rob H
0
 

Assisted Solution

by:gisvpn
gisvpn earned 0 total points
ID: 40022859
Hi Rob,

Thanks for the post - unfortunately this will be part of a wider VBA script and ideally I am looking to use this - the number of emails going out will be relatively large - a few hundred ;)

Regards,

GISVPN
0
 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 500 total points
ID: 40023003
Hi,

I am not up to scratch with cross application VBA but I know it can be done. SO maybe you could have part of the script open Word and create the mail merge.

Might as well use the tools for their strengths rather than butchering other tools to do the same job.

Similar question here using Excel and Word:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28418615.html

Thanks
Rob H
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Assisted Solution

by:gisvpn
gisvpn earned 0 total points
ID: 40023138
Hi Rob,

Thanks for the link to the other question - unfortunately the question does include a word document which I cannot use, I am not sure I can use the same technique for what I am doing? Do you have any suggestions on how I can create a loop down the worksheet and use the email script I already have above and add the date ?

GISVPN
0
 

Accepted Solution

by:
gisvpn earned 0 total points
ID: 40023779
Hi All,

Was working at this this afternoon and ended up with this which appears to work great.
Sub Email_Untrained_PM()


Dim PMRow As Integer
Dim ACellValue As String
Dim BCellValue As String
Dim CCellValue As String
Dim JCellValue As String
    
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String





PMRow = 4
 
    Do
    
   
        ACellValue = Sheets("PMs").Cells(PMRow, 1).Value
        BCellValue = Sheets("PMs").Cells(PMRow, 2).Value
        
    
        
       
        If ACellValue = "" Then
            Exit Do
        End If
        
        
                
        CCellValue = Sheets("PMs").Cells(PMRow, 3).Value
        JCellValue = Sheets("PMs").Cells(PMRow, 10).Value

            
        If CCellValue <> "" And JCellValue = "" Then
        
        
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

    strbody = "MESSAGE"
              


    On Error Resume Next
    With OutMail
        .To = CCellValue
        .CC = ""
        .BCC = ""
        .Subject = "some message in subject " & ACellValue & " " & BCellValue & ""
        .HTMLBody = strbody
        '.Send
        .Display
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing


         
       Sheets("PMs").Cells(PMRow, 10).Value = Now
       End If
       
       PMRow = PMRow + 1
              

    Loop
    




End Sub

Open in new window

0
 

Author Closing Comment

by:gisvpn
ID: 40031580
Please see best solution above
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now