+ Reply to Thread
Results 1 to 11 of 11

Macro Help: Sending Email from VBA

  1. #1
    Registered User
    Join Date
    11-18-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    7

    Macro Help: Sending Email from VBA

    Hello,

    I've got 2 workbooks. I need to write a macro that will be in workbook 1, and will requrie to attach workbook 1 to an email and deliver it to all receipients which have email addresses listed under "To"(A1), "CC"(B1), "BCC"(C1) columns as appropriate in workbook 2. The recipient list may need to expand or reduce at any time.

    Is this a simple process?

    Any help would be much appreciated.

    Thanks,

    Gerry

  2. #2
    Registered User
    Join Date
    05-26-2014
    Posts
    9

    Re: Macro Help: Sending Email from VBA

    Yes we can and I don't think it's complex! I have given you the draft (untested) version.

    Dim Subject,
    Send_From,
    Body As String

    Dim Mail_Object,
    Mail_Single As Variant

    Dim Rowcount,
    i as integer

    Workbooks.open("Workbook2.xls")
    Workbooks("Workbook2.xls").worksheet("Sheet1").activate

    With ActiveSheet
    Rowcount = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim Send_To(Rowcount) as string
    Dim Cc(Rowcount) as string
    Dim Bcc(Rowcount) as string

    Subject = "Trying to send email using VBA"
    Send_From = "[email protected]"
    Body = "You have successfully sent an e-mail using VBA !!!!"

    for i = 1 to Rowcount
    Send_To(i) = activesheet.cells(i,1)
    Cc(i) = activesheet.cells(i,2)
    Bcc(i) = activesheet.cells(i,3)

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)

    With Mail_Single
    .Subject = Subject
    .To = Send_To
    .cc = Cc
    .BCC = Bcc
    .Body = Body
    .send
    End With
    next i

    debugs:
    If Err.Description <> "" Then MsgBox Err.Description

  3. #3
    Registered User
    Join Date
    11-18-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro Help: Sending Email from VBA

    Dammy,

    Thanks for having a crack at this - much appreciated. Please excuse my lack of VBA skills here (ie I may be doing something stupid).

    When I enter that code as a Macro in Excel, it immediately highlights the following lines of code in red:

    Dim Subject,
    Send_From,

    Dim Mail_Object,

    Dim Rowcount,


    Attempting to run the macro gives the following error:

    Compile Error: Syntax Error

    By removing the commas at the end of the lines, the error will change to Compile Error: Sub or Function Not Defined on the line of code Body As String.

    By changing this to DIM Body As String, I was able to get the macro to run to the line of code Mail_Single As Variant, where the error Compile Error: Statement invalid outside Type Block occurred.

    Not entirely sure what I was doing by making these changes, just thought I'd play around with it to see what I can do myself.

    Looking forward to any help that anybody can provide to take this forward.

    Thanks,

    Gerry

  4. #4
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Macro Help: Sending Email from VBA

    Quote Originally Posted by Gbrown92 View Post
    Dammy,

    Thanks for having a crack at this - much appreciated. Please excuse my lack of VBA skills here (ie I may be doing something stupid).

    When I enter that code as a Macro in Excel, it immediately highlights the following lines of code in red:

    Dim Subject,
    Send_From,

    Dim Mail_Object,

    Dim Rowcount,


    Attempting to run the macro gives the following error:

    Compile Error: Syntax Error

    By removing the commas at the end of the lines, the error will change to Compile Error: Sub or Function Not Defined on the line of code Body As String.

    By changing this to DIM Body As String, I was able to get the macro to run to the line of code Mail_Single As Variant, where the error Compile Error: Statement invalid outside Type Block occurred.

    Not entirely sure what I was doing by making these changes, just thought I'd play around with it to see what I can do myself.

    Looking forward to any help that anybody can provide to take this forward.

    Thanks,

    Gerry
    I changed a few things that will hopefully fix the problems

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-18-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro Help: Sending Email from VBA

    dmasters4919,

    That code seems to be an improvement. Only one line of code was red:

    Please Login or Register  to view this content.
    Surely this is because "To" is not defined anywhere? Does this mean I need to use a DIM statement to define it in some way?

    Thanks for the help so far. I'm going to learn VBA on the back of this so I can help myself next time!

    Gerry

  6. #6
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Macro Help: Sending Email from VBA

    Quote Originally Posted by Gbrown92 View Post
    dmasters4919,

    That code seems to be an improvement. Only one line of code was red:

    Please Login or Register  to view this content.
    Surely this is because "To" is not defined anywhere? Does this mean I need to use a DIM statement to define it in some way?

    Thanks for the help so far. I'm going to learn VBA on the back of this so I can help myself next time!

    Gerry
    There shouldn't be any red lines if it's pasted correctly. I tested the entire thing and it worked... What exactly is the error given when you press F8 to start the sub?

  7. #7
    Registered User
    Join Date
    11-18-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro Help: Sending Email from VBA

    Oops! It was pasted incorrectly - my mistake. This seems to have worked nicely but I've only got one email in the list. I'll add the circulation properly and feed back. The only thing it now needs to do is attach workbook 1. Can this be implemented easily?

    Thanks

  8. #8
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Macro Help: Sending Email from VBA

    Quote Originally Posted by Gbrown92 View Post
    Oops! It was pasted incorrectly - my mistake. This seems to have worked nicely but I've only got one email in the list. I'll add the circulation properly and feed back. The only thing it now needs to do is attach workbook 1. Can this be implemented easily?

    Thanks
    Oh yes I forgot you wanted it to be attached too. That is simple. Insert an attach line after .body, followed by the FULL file path (same one you use to open a workbook. So it would look like this

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-26-2014
    Posts
    9

    Re: Macro Help: Sending Email from VBA

    oh yes! sorry for the mess as i drafted the code in hurry!! but dmasters4919's corrected solution would work fine!!!

  10. #10
    Registered User
    Join Date
    11-18-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro Help: Sending Email from VBA

    Sorry to resurrect an old thread - but I moved away from this code for a while and have just picked it back up.

    I have managed to adapt the code that you have given me to suit my needs (almost!) - thanks. The last remaining problem is that the macro sends individual emails to data in each row of the spreadsheet because of the way that it loops.

    I'm looking for it to send one email to all of the contacts within the range at once. Can you help with this? A sample of the code I've got is:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    05-26-2014
    Posts
    9

    Post Re: Macro Help: Sending Email from VBA

    Happy that it worked and just modify the loop as shown below,

    For i = 2 To Rowcount
    Send_To = Send_To & ";" & ActiveSheet.Cells(i, 1)
    Cc = Cc & ";" & ActiveSheet.Cells(i, 2)
    Bcc = Bcc & ";" & ActiveSheet.Cells(i, 3)
    Next i

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)

    With Mail_Single
    .Subject = Subject
    .To = Send_To
    .Cc = Cc
    .Bcc = Bcc
    .Body = Body
    .Attachments.Add "Workbook.xls"
    .Send
    End With

    Hope it will serve ur purpose.

    Thanks,
    Dammy.

+ 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. Macro sending email from different email address.
    By learningslowly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2014, 03:24 PM
  2. automation/macro for sending email to multiple email address
    By saurabhlotankar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2013, 12:13 PM
  3. Email sending macro
    By saurabhlotankar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-18-2013, 04:37 AM
  4. Sending Email by Macro.
    By dare2join in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-16-2013, 03:09 AM
  5. Sending Email with Macro
    By Vincent.Eymard in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2010, 09:37 AM

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