+ Reply to Thread
Results 1 to 3 of 3

Problem with generating multiple email with message body, subject and emails automatically

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    16

    Problem with generating multiple email with message body, subject and emails automatically

    I am very new to programming. I have been working on this for months, but i still couldnt solve it.
    My task here is to generate email automatically.
    When i enter x to run the sub findvalue macro.
    Any cells on the column D that has the value of 10 should generate email with the message body, subject and email address automatically.
    example if there are 3 task that are 10 days to deadline, 3 email will be generated after entering "x"
    I have edited the Sendmail sub to locate the email's body, subject line and email from the excel.
    that is the ideal case, however, i have a problem executing the macro. I don't really understand the whole code so i need find in this.
    Please look at the attached workbook.
    Any help is appreciated


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Cells.Count > 1 Then Exit Sub
            If Target = "x" Then
        If Not Intersect(Target, Target.Worksheet.Range("G2")) Is Nothing Then
        Call Findvalue
        End If
        End If
      
    End Sub
    Sub Findvalue()
    
    Dim Rng1 As Range
    Dim foundemail As Range
    Dim a As Variant
    
    Set Rng1 = Range("D2:D10")
    For Each a In Rng1
    If a.Value = 10 Then
        
        Set foundemail = Sheets("Email").Range("A:A").Find(What:=Cells(Target.Row, 1), _
                                                           LookAt:=xlWhole, _
                                                           MatchCase:=False)
                                                              
        If foundemail Is Nothing Then
        MsgBox "Cannot match name on Email address sheet. ", vbExclamation, "No Match Found"
                    Else
                        Call Sendmail(foundemail.Offset(, 1).Value2)
                     
                        End If
                        End If
                        Next
                        End Sub
    Sub Sendmail(Email As String)
         
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        sCC = ""
        sBCC = ""
        sSubject = "Reminder! Task Overdue"
        strbody = "Dear " & Cells(ActiveCell.Row, 1) & "," & vbNewLine & vbNewLine & "Task:" & "  " & Cells(ActiveCell.Row, 2) & vbNewLine & vbNewLine & "Due Date:" & "  " & Cells(ActiveCell.Row, 3) & vbNewLine & vbNewLine & " Thank You. "
        With OutMail
            .To = Email
            .CC = sCC
            .BCC = sBCC
            .Subject = sSubject
            .Body = strbody
            .Display
        End With
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Problem with generating multiple email with message body, subject and emails automatic

    You can't use Target outside of the sheet module.

    Pass the loop object to the email sub so you can insert data required.

    Dim foundemail As Range
    Dim a As Range
    
    Set Rng1 = Range("D2:D10")
    For Each a In Rng1
    If a.Value = 10 Then
        
        Set foundemail = Sheets("Email").Range("A:A").Find(What:=Cells(a.Row, 1), _
                                                           LookAt:=xlWhole, _
                                                           MatchCase:=False)
                                                              
        If foundemail Is Nothing Then
        MsgBox "Cannot match name on Email address sheet. ", vbExclamation, "No Match Found"
                    Else
                        Call Sendmail(a, foundemail.Offset(, 1).Value2)
                     
                        End If
                        End If
                        Next
                        End Sub
    Sub Sendmail(ObjA As Range, Email As String)
         
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        sCC = ""
        sBCC = ""
        sSubject = "Reminder! Task Overdue"
        strbody = "Dear " & ObjA.Offset(, -3) & "," & vbNewLine & vbNewLine & _
            "Task:" & "  " & ObjA.Offset(, -2) & vbNewLine & vbNewLine & _
            "Due Date:" & "  " & ObjA.Offset(, -1) & _
            vbNewLine & vbNewLine & " Thank You. "
        With OutMail
            .To = Email
            .CC = sCC
            .BCC = sBCC
            .Subject = sSubject
            .Body = strbody
            .Display
        End With
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    04-22-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Problem with generating multiple email with message body, subject and emails automatic

    Thanks for your help! finally i get some result.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1