+ Reply to Thread
Results 1 to 16 of 16

Send mails with attachments using a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Question Send mails with attachments using a macro

    Hey Guys,

    I'm looking for a macro which will send some emails for me using outlook. But I just can't find what I'm searching for (I've been looking for hours now).
    It's kinda complicated in my opinion, but you guys can probably figure this out. (atleast I hope so)

    This is what I want to achieve:

    I want the macro to fill in the receiver from column E
    Than I need it to fill in the CC field with a adres specified in column G
    Than I would like to add the subject from cell J5 (always the same cell so I can change the subject manually for all mails at once)
    Than I want to attach the file, from which the location (localy stored) is specified in column B

    Now the first part is done, I want to write my email message.
    There are 2 different mails, one in english and one in dutch (my main language), which language the receiver speaks is specified in column H.

    So if column H says "English" I want it to add the text from cell J1 to my mail, this cell says "Dear " (if Dutch I need it to add text from J2, saying "Geachte "
    Now I need the macro to add the name from the receiver, which is specified in column C. (this should make the first sentense say "Dear John" for example

    Now for the rest of the mail I need the macro to look up column H again, if that cell says "English" it needs to add the text from cell J3 which contains the rest of the message in HTML format. (if the cell in column H says "Dutch" it needs to add text from cell J4 which contains the same message in HTML format, but than in dutch)

    I need the macro to send a file for every row filled with information in column A. (this is column that changes, I load a list with people I need to mail into that column, all other columns have VLOOPUP's to fill in the appropiate information (email adresses names etc.)

    If someone could help me out on this it would mean the world to me.

    Thanks in advance,
    Jerry
    Last edited by Warmerfare; 03-24-2017 at 11:21 AM.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    I'd suggest starting with this page
    Incidentally I believe he is a fellow dutchman
    It is a very good source for mailing with outlook.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Hey Arkadi,

    First of all, thanks for the quick reply.
    Funny fun is, it's exactly the site I was looking at, but I can't figure out how to do this.

    To be more specific, instead of having the subject written in the macro, I need the macro to look up what is in column H (EN or NL). Than I want to have the macro select a cell containing my subject in the desired language based on the information given in column H.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    Ok, but do you have a basic structure already for the emailing?
    I'm happy to help with the details of course.
    If you can provide a copy of the workbook with some test data, I can probably be more useful?
    Last edited by Arkadi; 03-24-2017 at 09:30 AM.

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    You said CC is from column H, but you also say "If column H says Dutch then..."
    Last edited by Arkadi; 03-24-2017 at 10:03 AM.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    Beste Warmerfare, here is something to start with:

    Option Explicit
    Sub Send_Files()
    
    Dim OutApp As Object
    Dim OutMail As Object
    Dim ws As Worksheet
    Dim i As Long
    Dim lr As Long
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Set ws = Sheets("Sheet1")
    lr = ws.Range("A" & Rows.Count).End(xlUp).Row 'finds last row with data in column A, since you said send for every row with data in A
    Set OutApp = CreateObject("Outlook.Application")
    
    For i = 2 To lr 'assuming headers in row 1, loop starts at row 2
    
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = ws.Range("E" & i).Value 'using E for "To" email adress
            .CC = ws.Range("G" & i).Value 'update to column with CC address
            .Subject = ws.Range("J5").Value
            If ws.Range("H" & i).Value = "English" Then
                .htmlbody = ws.Range("J1").Value & ws.Range("C" & i).Value & "<br><br>" & ws.Range("J3").Value
            ElseIf ws.Range("H" & i).Value = "Dutch" Then
                .htmlbody = ws.Range("J2").Value & ws.Range("C" & i).Value & "<br><br>" & ws.Range("J4").Value
            End If
            'if there is a file specified in B then add attachment
            If ws.Range("B" & i).Value <> vbNullString Then .Attachments.Add ws.Range("B" & i).Value
            .Send  'Or use .Display
        End With
        
        Set OutMail = Nothing
    Next i
    
    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    End Sub
    Last edited by Arkadi; 03-24-2017 at 11:48 AM.

  7. #7
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Mhh I just dont have patience. So I parked my car, opened my laptop, logged into my phone's wifi and tested this VBA/Macro. And it works!!!
    The only thing is that it does not send the message inside of the mail, but this is probably due to using plain text instead of HTML in my excel. Gonna fix that as soon as I'm home... Can't wait

    Thankyou

  8. #8
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Hey Arkadi,

    Your right, it should've been column G instead of H for the CC field. I've changed that in my Original post as wel. Thanks for letting me know

    And 100 times thanks for the VBA, it seems like this is the code I was looking for (and couldn't write myself). I'm currently driving for a few more hours, but I'll test it as soon as I arrive home (and of course let you know if it works )

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    Driveing for a few hours??? That is enough to drive around all of Nederland a few times!! LOL

    I updated my code post with G for the CC field. Drive safe, and hope it works out for you.

    If you used plain text that should still have worked as part of the email message... are the Cells J3/J4 correct? Did it at least put Dear/Geachte, and the name?

  10. #10
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14
    Lol that's true but I need to go by a few adresses, and dutch trafic is as good as non moving during these hours.

    back ontopic: the mail didnt contain any message. So there's no dear ,name, or message.
    the macro needs some work I quess

  11. #11
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    When I tested it on my computer it worked fine... but I wonder.... my code is based on the worksheet being called "Sheet1" is that the name of your sheet with data? If the sheet has a different name then you must change
    Set ws = Sheets("Sheet1")
    to use the correct worksheet name.

    Otherwise, can you provide a workbook with 1 or 2 lines of data? emails and names don't need to be real, just structure must be the same, and some kind of value in the fields for language name and message.

  12. #12
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Mhhh my sheet is named sheet1, It was called Variable Data but saw that line of code when I read trough it, so I changed it to Sheet1 again. my vlookup data is located on sheet2 which is still named as Static Data, but that would not make a difference as far as I know since all the data the macro looks up is printed on sheet1. Or am I wrong here?

    will post the file monday morning. I currently can't acces it (or I will reproduce and post it tomorrow)

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    Warmefare je hebt idd gelijk.... Zolang de sheet Sheet1 heet moet het eigenlijk goed gaan. Maar ja... Als je het blad kan sturen dan zetten we dit wel in orde

  14. #14
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Hey arkadi, ik heb de file even nagemaakt.

    De eerste 2 kolommen worden gevult door een macro die de leveranciersnaam en de file path naar het juiste bestand invoegt. En uiteraard zijn er email adressen ingevult bij de file op mijn werk.

    http://www.filedropper.com/autoemail

    Ik hoop dat je er iets mee kan, en alvast super bedankt voor al je hulp

  15. #15
    Registered User
    Join Date
    03-17-2017
    Location
    Amsterdam
    MS-Off Ver
    2007
    Posts
    14

    Re: Send mails with attachments using a macro

    Hey,

    Ik heb gevonden wat er mis was, voor het nederlandse bericht werd de taal van communicatie uit vakje H gehaald, terwijl deze in vakje I aangegeven stond

    Na het aanpassen word het bericht gewoon in de mail geplakt, alleen niet in de juiste opmaak

    Hij slaat enters over en plakt de gehele tekst achter elkaar, maar daar ga ik nog heel even mee spelen Gefixt, heb de email-tekst in html in de macro gezet, en hem als een string op laten roepen. Een voor engels, en een voor nederlands.

    Superbedankt voor al je hulp Arkari. Je bent een held !
    Last edited by Warmerfare; 03-27-2017 at 09:45 AM.

  16. #16
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Send mails with attachments using a macro

    Je had gezegd dat het bericht op het blad al in in HTML was.... maar in html zijn "enters" "<br>". Is dat misschien het probleem?

    probeer anders maar

    If ws.Range("I" & i).Value = "EN" Then
                .htmlbody = ws.Range("J1").Value & ws.Range("C" & i).Value & "<br><br>" & Replace(ws.Range("J3").Value, Chr(10), "<br>")
            ElseIf ws.Range("I" & i).Value = "NL" Then
                .htmlbody = ws.Range("J2").Value & ws.Range("C" & i).Value & "<br><br>" & Replace(ws.Range("J4").Value, Chr(10), "<br>")
            End If
    het is ook mogelik dat het chr(13) is in plaats van 10.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. I want to set up a macro? that will send e-mails from Excel based on a date
    By Chuck@kayaknw.org in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-26-2016, 01:42 PM
  2. I want to set up a macro? or to send e-mails from Excel
    By Chuck@kayaknw.org in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-26-2016, 01:05 PM
  3. Send multiple emails with different attachments if attachments found
    By AnkitJain in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2015, 05:35 AM
  4. [SOLVED] Macro to send mails from outlook as per the email ids in excel
    By arun.sj in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 07-03-2015, 12:10 PM
  5. Excel containing macro to send bulk mails from outlook with multiple attachments
    By amandeep08 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2013, 02:19 PM
  6. add attachments and send macro buttons
    By ea223 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2013, 04:14 PM
  7. Macro for lotus to send individual mails to various receiptents
    By senthilkumar.k.chinn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-22-2011, 12:41 PM

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