Nov 12th, 2007, 10:41 AM
#1
Thread Starter
Junior Member
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("B4 6"), 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
Nov 12th, 2007, 02:05 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 04:13 AM
#3
Thread Starter
Junior Member
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
Nov 13th, 2007, 04:22 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 04:40 AM
#5
Thread Starter
Junior Member
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.
Nov 13th, 2007, 04:47 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 04:57 AM
#7
Thread Starter
Junior Member
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
Nov 13th, 2007, 05:26 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 05:39 AM
#9
Thread Starter
Junior Member
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...
Nov 13th, 2007, 05:43 AM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 05:53 AM
#11
Thread Starter
Junior Member
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)
Nov 13th, 2007, 06:01 AM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 13th, 2007, 06:08 AM
#13
Thread Starter
Junior Member
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
Nov 14th, 2007, 05:01 AM
#14
Thread Starter
Junior Member
Re: Auto Send Mail from Excel
HI RobDog
Did you have any joy with the example i sent?
thanks
B
Nov 16th, 2007, 10:48 AM
#15
Thread Starter
Junior Member
Re: Auto Send Mail from Excel
Thanks for looking at this but I have now found a way to resolve the problem.
B
Nov 16th, 2007, 01:00 PM
#16
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 19th, 2007, 04:09 AM
#17
Thread Starter
Junior Member
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
Forum Rules
Click Here to Expand Forum to Full Width