+ Reply to Thread
Results 1 to 3 of 3

Problem using a macro to send multiple emails with separate attachments

  1. #1
    Registered User
    Join Date
    04-25-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    2

    Problem using a macro to send multiple emails with separate attachments

    Hi there,

    I've written some basic code to send emails from a list of data in Excel but am having trouble getting the last part right.

    Code is:

    Sub mail()

    ' Select cell A2, *first line of data*.
    Range("A2").Select
    ' Set Do loop to stop when an empty cell is reached.
    Do Until IsEmpty(ActiveCell)

    Set Mail_Object = CreateObject("Outlook.Application")
    With Mail_Object.CreateItem(o)
    .Subject = "Subject goes here"
    .To = Range("b2")
    .Body = "Body text goes here"
    .Attachments.Add ActiveSheet.Range("c2").Text
    .Display

    End With


    ' Step down 1 row from present location.
    ActiveCell.Offset(1, 0).Select
    Loop
    End Sub

    Column A shows a name, column B shows an email address, column C shows the location of the file to attach.

    As you can see, at the moment when I run the code I get the same email to address and attachment on every email it creates because I'm referencing the specific cell.

    I'm sure it's very simple but all I need is for the macro to work its way down the list changing the email address and attachment location as it does so (i.e. for the first email it references a2,b2,c2 but then for the next mail references a3,b3,c3, and then a4,b4,c4 etc.)

    If someone could point me in the right direction for this I'd be extremely grateful.

    Many thanks,

    j.

  2. #2
    Registered User
    Join Date
    09-02-2009
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    77

    Re: Problem using a macro to send multiple emails with separate attachments

    Replace your .To line with:
    Please Login or Register  to view this content.
    And replace your .Attachments line with:
    Please Login or Register  to view this content.
    (You could do this without moving the selection down through the rows, but the above's the quickest way to get your code working.)

    EDIT: Warning - if you have more than a few rows of data, your screen will get very busy with emails very quickly. More than a couple dozen and you're in Sorcerer's Apprentice mode. Instead of displaying you could tell Outlook to save a draft; can't remember off the top of my head the particular method for this, but GIYF.
    Last edited by blackworx; 04-25-2013 at 01:47 PM.

  3. #3
    Registered User
    Join Date
    04-25-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Problem using a macro to send multiple emails with separate attachments

    That's perfect, thanks a lot, really helpful.

    As you said, .display turned out to be a pretty awful way of running this because there will be hundreds of lines of data so have now revised it as follows and it seems to work pretty well:

    Please Login or Register  to view this content.
    It now saves all mails to the drafts folder and sets the sensitivity level.

    Thanks again for your help, much appreciated.

+ 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