Pages

Tuesday, June 26, 2012

VBA Macro to send email with attachment as per the recepient list in excel sheet

1) Open New Excel sheet and mention user Name , Email ID & Score/Report in sequential cells in second row as first will cover their titles
2) copy and paste below code into the relevant macro window , choose any attachment by editing code accordingly and run it to see the magic 



Sub SendEmail()
 'Works in 2000-2007
    Dim EmailDist As String
    Dim AcWB As Workbook
    Dim OutApp As Object
    Dim OutMail As Object
    Dim str As String
    Dim r As Integer, x As Double
    Dim xint As Long
   
        For r = 2 To 4
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)

    'Date the Email is being sent
    'strDate = Format(Date, "dddd, mmm d yyyy")
 
    'Distribution list

    strAddress = Cells(r, 2)
 
    strSubject = "Your Online Exam Score"
    strBody = ""
         
     On Error GoTo JUSTEND
     With OutMail
        .To = strAddress
        .Subject = strSubject
        .HTMLBody = "Please Check the attachment"
         'Attach file here with complete path to file plus filename and file extension.
         .Attachments.Add "C:\abc.txt"
         .Send
            End With
JUSTEND:
         
    Set OutMail = Nothing 'Remove from memory
    Set OutApp = Nothing 'Remove from memory
     Next r
 
End Sub

No comments:

Post a Comment