Results 1 to 17 of 17

Thread: Auto Send Mail from Excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Auto Send Mail from Excel

    Hi there

    I am in the process of writing a macro that splits data on a spreadsheet, copies it into a new sheet, and then emails the new sheet to the relevant person before deleting the data.

    I have written the below code and the macro appears to work fine, the only problem being that no emails are actually being sent from my Outlook account. I am receiving the message stating that ' a program is trying to send email on your behalf', which indicates that it is trying to send the mail, and I select 'yes', but no mail is actually going out. i have nothing in my sent items and as i am testing the system I have put my own email address in there and I am not receiving any either.

    Can anyone help point me in the right direction? I'm not suer where I have gone wrong!

    thanks!


    Sub SendActiveWorkbook()

    Dim sname As String, i As Integer
    Dim OutApp As Object
    Dim OutMail As Object
    Dim EmailAddr As String
    Dim Subj As String
    Dim BodyText As String


    Workbooks.Open Filename:= _
    "C:\Documents and Settings\5ERINRED\Desktop\PMS Renewal Oct.xls"

    For i = 1 To 3
    sname = Choose(i, "ACK", "ACO", "ADB")
    On Error Resume Next

    Windows("October1.xls").Activate
    Selection.AutoFilter Field:=3, Criteria1:=sname
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("PMS Renewal Oct.xls").Activate
    ActiveSheet.Paste
    Range("E1").Activate
    [E2].End(xlDown).Offset(1, 0) = "=Sum(E2:E" & [E2].End(xlDown).Row & ")"
    Columns("E").Select
    Selection.NumberFormat = "$#,##0"
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A2").Select
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    With OutMail
    .To = Application.VLookup(Range(sname), Sheets("Adviser Names").Range("B4:F6"), 5, False)
    .CC = ""
    .BCC = ""
    .Subject = "PMS Renewal October"
    .Body = "Dear" & Application.VLookup(Range(sname), Sheets("Adviser Names").Range("B46"), 3, False) & vbCr & vbCr & _
    "Please find attached your PMS Renewal Stats for October." & vbCr & vbCr & "Thanks," & vbCr & vbCr & "Beks"
    .Attachments.Add ActiveWorkbook
    .Send
    End With

    Cells.Select
    Selection.ClearContents
    Windows("October1.xls").Activate
    Next i
    End Sub

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Thanks RobDog,

    The problem I have is that the spreadsheet needs to be filtered for each individual, this selection copied into a tenmplate, and the template emailed to the individual. I tried using the SendMail command but it appears to ignore the VLOOKUP I have entered for the email address, which is determined by the choose command which also selects the correct filter.

    When I ran it this time it did not even attempt to send the mail. Do you have any further suggestions?

    Thanks again!

    B

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    Prep calling the sendmail function by making those calls first and set to variables perhaps. Then pass the vars to the sendmail function.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Thanks RobDog

    I've now got it to the stage where it is running the macro and bringing up the email with the sheet attached, but not entering the email address. do you know how I can set the variables to make this work?

    The email address is found using a VLOOKUP, which looks up the value of sname for that i each time the macro loops.

    I am fairly new to VB and am just struggling with this last bit!

    B.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    Post your updated code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Thanks Robdog,

    Code attached as it works so far. This runs right through to the email with the attachment, but just doesn't select the relevant email address:

    Sub SendActiveWorkbook()

    Dim sname As String, i As Integer
    Dim OutApp As Object
    Dim OutMail As Object
    Dim EmailAddr As String
    Dim Subj As String
    Dim BodyText As String


    Workbooks.Open Filename:= _
    "C:\Documents and Settings\5ERINRED\Desktop\PMS Renewal Oct.xls"

    For i = 1 To 3
    sname = Choose(i, "ACK", "ACO", "ADB")
    On Error Resume Next

    EmailAddr = Application.VLookup(Range(sname), Sheets("Adviser Names").Range("B4:F6"), 5, False)

    Windows("October.xls").Activate
    Selection.AutoFilter Field:=3, Criteria1:=sname
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("PMS Renewal Oct.xls").Activate
    ActiveSheet.Paste
    Range("E1").Activate
    [E2].End(xlDown).Offset(1, 0) = "=Sum(E2:E" & [E2].End(xlDown).Row & ")"
    Columns("E").Select
    Selection.NumberFormat = "$#,##0"
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A2").Select
    ActiveWorkbook.SendMail EmailAddr, "PMS October"
    Cells.Select
    Selection.ClearContents
    Windows("October.xls").Activate
    Next i
    End Sub

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    Make sure th variable is holding the value. Take out the "On Error Resume Next" as that hides issues.

    Try this sample to see it if populates for you, it does for me.

    Code:
        Dim blah As String
        Dim blah2 As String
        blah = "[email protected]"
        blah2 = "Subject"
        ActiveWorkbook.SendMail blah, blah2
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Thanks. I have taken out the Error skip and it is erroring on this line:

    EmailAddr = Application.VLookup(Range(sname), Sheets("Adviser Names").Range("B4:F6"), 5, False)

    The problem is it is not using the vlookup to return the email address in the string. It works fine if you acutally write the email address into the code, but I want it to find the email address in the worksheet based on the item it chooses in the sname = Choose line of code...

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    I see you are referencing the Range function without fully qualifying its parent sheet. This may make it read a different location and return an error.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    I changed it to this but I am still getting an error! It is now saying there is an application-defined or object-defined error??



    EmailAddr = Application.VLookup(Sheets("Adviser Names").Range(sname), Sheets("Adviser Names").Range("B4:F6"), 5, False)

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    What is the value for sname? What does B4:F6 look like/values?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Here is a zipped version of the spreadsheet with the macro in it. The PMS Renewal October sheet the macro refers to is just a blank spreadsheet used as a template.

    Hope this helps!
    Attached Files Attached Files

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    HI RobDog

    Did you have any joy with the example i sent?

    thanks

    B

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Resolved Re: Auto Send Mail from Excel

    Thanks for looking at this but I have now found a way to resolve the problem.

    B

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Send Mail from Excel

    No, I havent had any time. What did you do to resolve it?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    19

    Re: Auto Send Mail from Excel

    Hi there

    I used a second choose function for the selection of the email address. It's not the way I would have prefered to do it but it does work!

    thanks.

    B

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width