Sending general mails with a common attachment

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Sending general mails with a common attachment

Post by Ananthan »

Dear Team,

I need a macro to send bulk mails where mail id willl be in excel & subject & content in the mail body will be common, And also a pdf as attachment.

I am having one but need to rectify it

Code: Select all

Sub Mail()
    Dim r As Long
    Dim m As Long
    Dim objOL As Object
    Dim objMsg As Object
    Dim blnStart As Boolean

    ' Try to open Outlook
    On Error Resume Next
    Set objOL = GetObject(Class:="Outlook.Application")
    If objOL Is Nothing Then
        Set objOL = CreateObject(Class:="Outlook.Application")
        If objOL Is Nothing Then
            MsgBox "Failed to start Outlook!", vbCritical
            Exit Sub
        End If
        blnStart = True
    End If
    On Error GoTo ErrHandler
    objOL.Session.Logon

    ' Determine last used row
    m = Range("B" & Rows.Count).End(xlUp).Row
    ' Loop through the rows
    For r = 2 To m
        ' Check if e-mail address is filled in
        If Range("B" & r).Value <> "" Then
            ' Create message
            Set objMsg = objOL.CreateItem(0) ' olMailItem
            ' Recipient
            objMsg.Recipients.Add Range("B" & r)
            ' Subject
            objMsg.Subject = "Reminder"
            ' Body
            objMsg.Body = "Dear " & Range("A" & r).Value & "," & vbCrLf & vbCrLf & _
                "Greetings from " & Range("G" & r).Value & "," & vbCrLf & vbCrLf & _
                "We are happy to inform you that the " & Range("C" & r).Value & _
                " you have taken from our branch of " & Range("F" & r).Text & _
                " is falling due on " & Range("D" & r).Text & _
                ". Please visit the branch to prolong our relationship or SMS to " & _
                Range("E" & r).Text & ". Our executive will contact you." & vbCrLf & vbCrLf & _
                "Regards," & vbCrLf & "ABC Ltd"
            ' Use only one of the next two instructions
            ' For testing: display the message
            objMsg.Display
            ' For the final version: send the message
            objMsg.Attachments.Add ("C:\test.txt")
             'objMsg.Send
        End If
    Next r

ExitHandler:
    On Error Resume Next
    If blnStart And Not objOL Is Nothing Then
        objOL.Quit
    End If
    Exit Sub

ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    ActiveWorkbook.Save
    
    ChDir "C:\Documents and Settings\Administrator\Desktop"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Documents and Settings\Administrator\Desktop\Test.xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
    ActiveWorkbook.Save
End Sub
Regards
Ananthan
Last edited by HansV on 27 May 2014, 05:08, edited 1 time in total.
Reason: to place [code] ... [/code] tags around code.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

If you want to attach a PDF file, you should specify the path and filename of the PDF file instead of ("C:\test.txt") in the line

Code: Select all

            objMsg.Attachments.Add ("C:\test.txt")
There doesn't seem to be any point in the code

Code: Select all

    ChDir "C:\Documents and Settings\Administrator\Desktop"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Documents and Settings\Administrator\Desktop\Test.xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Save
    ActiveWorkbook.Save
in the error handling section of the macro; I would omit those lines.
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Thanks , what we do if we get a syntax error, compile error, the message is littile biggr in size ????

Please advise..

Regards
Ananthan

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

On which line do you get the compile error?
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Or can you provide a modified code for this pl....

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

In the message body ....

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

Sorry, I don't understand what your problem is.
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Can you pl provide me a macro in which we can send a common message using outlook to various mail ids from excel. Only need to take mail ids from excel & subject & attachment will be same.

Regards
Ananthan

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

You already have that code, so I don't understand what you need.
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Dear Team,
I am facing an error while changing the mail body

Exactly here, I am able to change subject & attachment but this one is diiferent no need to pick from the cells specified, as I said before I am new to this so need your valuable guidance. Message is a common msg & mail ids nned to be picked from excel file thats all.

objMsg.Body = "Dear " & Range("A" & r).Value & "," & vbCrLf & vbCrLf & _
"Greetings from " & Range("G" & r).Value & "," & vbCrLf & vbCrLf & _
"We are happy to inform you that the " & Range("C" & r).Value & _
" you have taken from our branch of " & Range("F" & r).Text & _
" is falling due on " & Range("D" & r).Text & _
". Please visit the branch to prolong our relationship or SMS to " & _
Range("E" & r).Text & ". Our executive will contact you." & vbCrLf & vbCrLf & _
"Regards," & vbCrLf & "ABC Ltd"

Regards
Ananthan

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

What is the error?
What should be the text of the body?
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Thank you for giving us an opportunity to service you.

Now get complete “CARE “ FOR YOU & YOUR FAMILY
- Get Health checkups every year
- Auto recharge of sum insured
-Avail treatments anywhere in the world
-In house claim management
- Strong Legacy in Health Care
For further queries Pl feel free to contact Muthoot Finance Branch or SMS/ Call to 1234567890

Compile error/ syntax error is what I am getting

Regards
Ananthan

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

Quotes within a string should be doubled. Try this version:

Code: Select all

    objMsg.Body = "Thank you for giving us an opportunity to service you." & vbCrLf & vbCrLf & _
        "Now get complete ""CARE"" FOR YOU & YOUR FAMILY" & vbCrLf & _
        "- Get Health checkups every year" & vbCrLf & _
        "- Auto recharge of sum insured" & vbCrLf & _
        "- Avail treatments anywhere in the world" & vbCrLf & _
        "- In house claim management" & vbCrLf & _
        "- Strong Legacy in Health Care" & vbCrLf & vbCrLf & _
        "For further queries please feel free to contact " & _
        "Muthoot Finance Branch or SMS/Call to 1234567890"
Best wishes,
Hans

Ananthan
Lounger
Posts: 26
Joined: 23 Apr 2014, 06:05

Re: Sending general mails with a common attachment

Post by Ananthan »

Hi team,
Thanks a lot ,
Need to add signature also "Regards," & vbCrLf & "ABC 1234 Ltd"

Regards,
Ananthan

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Sending general mails with a common attachment

Post by Rudi »

Coding for your signature depends on the detail you need in your signature?

The most complete resource for adding signatures to an email is through Ron de Bruin's website.
See: Example 2 : Insert the signature that you want without picture (Half way down the page if its just a text signature, or the other options for including a picture)
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending general mails with a common attachment

Post by HansV »

If you simply want to add some text at the bottom, you can concatenate it with the body text:

Code: Select all

    objMsg.Body = "Thank you for giving us an opportunity to service you." & vbCrLf & vbCrLf & _
        "Now get complete ""CARE"" FOR YOU & YOUR FAMILY" & vbCrLf & _
        "- Get Health checkups every year" & vbCrLf & _
        "- Auto recharge of sum insured" & vbCrLf & _
        "- Avail treatments anywhere in the world" & vbCrLf & _
        "- In house claim management" & vbCrLf & _
        "- Strong Legacy in Health Care" & vbCrLf & vbCrLf & _
        "For further queries please feel free to contact " & _
        "Muthoot Finance Branch or SMS/Call to 1234567890" & vbCrLf & vbCrLf & _
        "Regards," & vbCrLf & _
        "ABC 1234 Ltd"
Best wishes,
Hans