Solved

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

Posted on 2014-04-25
6
257 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 33

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 33

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you how to use shortcut menus in the Access run-time environment.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

839 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