Solved

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

Posted on 2014-04-25
6
254 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 32

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 32

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This article will show you how to use shortcut menus in the Access run-time environment.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

930 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

11 Experts available now in Live!

Get 1:1 Help Now