VBA send email via Outlook using loop; No response when run
Solved/Closed
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
-
Updated on May 23, 2017 at 11:51 AM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - May 29, 2017 at 10:56 AM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - May 29, 2017 at 10:56 AM
Related:
- Vba outlook send email
- How to refresh outlook email - Guide
- Net send windows 11 - Guide
- Outlook free download - Download - Email
- No email no password - Guide
- Vba case like - Guide
3 responses
Ambucias
Posts
47310
Registration date
Monday February 1, 2010
Status
Moderator
Last seen
February 15, 2023
11,163
May 18, 2017 at 04:49 PM
May 18, 2017 at 04:49 PM
I don't see any code above. What about you?
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
May 23, 2017 at 08:40 AM
May 23, 2017 at 08:40 AM
Hi Ambucias,
Thank you for your response.
I am trying to write a VBA code to send auto email reminders from an excel sheet however I am getting some error. Below is the code:
Sub SendEmail()
Dim olapp As Outlook.Application
Set olapp = CreateObject("Outlook.Application")
Dim olmail As Outlook.MailItem
Set olmail = olapp.CreateItem(olMailItem)
Dim maildest As String
Dim i As Integer
For i = 2 To 5
maildest = ""
maildest = Cells(i, 5).Value
olmail.To = maildest
olmail.Subject = "is it running yet?"
olmail.Body = "A gentle reminder that XXX tasks are still pending"
olmail.Send
Next i
Range("A8").Select
Set olmail = Nothing
Set olapp = Nothing
End Sub
Could you kindly guide as to what is wrong with the code? The loop never functions.
Thank you and look forward to your help
Best,
Dominic
Thank you for your response.
I am trying to write a VBA code to send auto email reminders from an excel sheet however I am getting some error. Below is the code:
Sub SendEmail()
Dim olapp As Outlook.Application
Set olapp = CreateObject("Outlook.Application")
Dim olmail As Outlook.MailItem
Set olmail = olapp.CreateItem(olMailItem)
Dim maildest As String
Dim i As Integer
For i = 2 To 5
maildest = ""
maildest = Cells(i, 5).Value
olmail.To = maildest
olmail.Subject = "is it running yet?"
olmail.Body = "A gentle reminder that XXX tasks are still pending"
olmail.Send
Next i
Range("A8").Select
Set olmail = Nothing
Set olapp = Nothing
End Sub
Could you kindly guide as to what is wrong with the code? The loop never functions.
Thank you and look forward to your help
Best,
Dominic
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
May 23, 2017 at 11:11 AM
May 23, 2017 at 11:11 AM
Hi x.dominicraj,
Try changing the following:
Dim olapp As Outlook.Application
into:
Dim olapp As Object
Dim olmail As Outlook.MailItem
into:
Dim olmail As Object
Set olmail = olapp.CreateItem(olMailItem)
into:
Set olmail = olapp.CreateItem(0)
When it is still not working try adding the line:
On Error GoTo 0
after the 'Next i' line.
Hope that helps.
Best regards,
Trowa
Try changing the following:
Dim olapp As Outlook.Application
into:
Dim olapp As Object
Dim olmail As Outlook.MailItem
into:
Dim olmail As Object
Set olmail = olapp.CreateItem(olMailItem)
into:
Set olmail = olapp.CreateItem(0)
When it is still not working try adding the line:
On Error GoTo 0
after the 'Next i' line.
Hope that helps.
Best regards,
Trowa
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
Updated on May 23, 2017 at 11:28 AM
Updated on May 23, 2017 at 11:28 AM
HI TrowaD,
Thank you for your response.
I made the changes as you had mentioned.
Now it gives the following error :
Runtime error : '-2147221238 (8004010a)':
The item has been moved or deleted.
When I click the debug option it highlights the line : "olmail.To = maildest"
The code is able to shoot mail just to the 1st address in the excel sheet. For some reason the loop is not running the cycle beyond counter 1. Any suggestions? Thanks
Best,
Dominic
Thank you for your response.
I made the changes as you had mentioned.
Now it gives the following error :
Runtime error : '-2147221238 (8004010a)':
The item has been moved or deleted.
When I click the debug option it highlights the line : "olmail.To = maildest"
The code is able to shoot mail just to the 1st address in the excel sheet. For some reason the loop is not running the cycle beyond counter 1. Any suggestions? Thanks
Best,
Dominic
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
>
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
May 23, 2017 at 11:45 AM
May 23, 2017 at 11:45 AM
Hi x.dominicraj,
Ok, move the following code line:
Set olmail = olapp.CreateItem(0)
and place it below:
For i = 2 To 5
That line needs to be within the loop.
Best regards,
Trowa
Ok, move the following code line:
Set olmail = olapp.CreateItem(0)
and place it below:
For i = 2 To 5
That line needs to be within the loop.
Best regards,
Trowa
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
May 23, 2017 at 11:51 AM
May 23, 2017 at 11:51 AM
Hi TrowaD,
Thank you for your response.
The code now looks like this:
Sub SendEmail()
Dim olapp As Object
Set olapp = CreateObject("Outlook.Application")
Dim olmail As Object
Dim maildest As String
Dim i As Integer
For i = 2 To 5
Set olmail = olapp.CreateItem(0)
maildest = ""
maildest = Cells(i, 5).Value
olmail.To = maildest
olmail.Subject = "is it running yet?"
olmail.Body = "A gentle reminder that XXX tasks are still pending"
olmail.Send
Next i
On Error GoTo 0
Range("A8").Select
Set olmail = Nothing
Set olapp = Nothing
End Sub
Still shows the same error as mentioned above by me :
When I click the debug option it highlights the line : "olmail.To = maildest"
Any suggestions on this error?
Thanks.
Best,
Dominic
Thank you for your response.
The code now looks like this:
Sub SendEmail()
Dim olapp As Object
Set olapp = CreateObject("Outlook.Application")
Dim olmail As Object
Dim maildest As String
Dim i As Integer
For i = 2 To 5
Set olmail = olapp.CreateItem(0)
maildest = ""
maildest = Cells(i, 5).Value
olmail.To = maildest
olmail.Subject = "is it running yet?"
olmail.Body = "A gentle reminder that XXX tasks are still pending"
olmail.Send
Next i
On Error GoTo 0
Range("A8").Select
Set olmail = Nothing
Set olapp = Nothing
End Sub
Still shows the same error as mentioned above by me :
When I click the debug option it highlights the line : "olmail.To = maildest"
Any suggestions on this error?
Thanks.
Best,
Dominic
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
>
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
May 23, 2017 at 12:24 PM
May 23, 2017 at 12:24 PM
Hi x.dominicraj,
Are you sure the mail addresses in range E2:E5 are correct and still existing? Maybe try using the same address as in E2.
Also try to place
Set olmail = Nothing
at the end of the loop, so before 'Next i'
Besides that I'm out of suggestions.
At a last effort to help you out, here is a site with a lot of code examples for Outlook:
https://www.rondebruin.nl/win/s1/outlook/mail.htm
Do keep us posted on your findings.
Good luck and best regards,
Trowa
Are you sure the mail addresses in range E2:E5 are correct and still existing? Maybe try using the same address as in E2.
Also try to place
Set olmail = Nothing
at the end of the loop, so before 'Next i'
Besides that I'm out of suggestions.
At a last effort to help you out, here is a site with a lot of code examples for Outlook:
https://www.rondebruin.nl/win/s1/outlook/mail.htm
Do keep us posted on your findings.
Good luck and best regards,
Trowa
x.dominicraj
Posts
7
Registration date
Thursday May 18, 2017
Status
Member
Last seen
June 1, 2017
May 23, 2017 at 01:03 PM
May 23, 2017 at 01:03 PM
Hi Trowa,
Set olmail = Nothing did the trick. The mail is going to all the contacts in the column.
Thank you so much for your help!
Best Regards,
Dominic
Set olmail = Nothing did the trick. The mail is going to all the contacts in the column.
Thank you so much for your help!
Best Regards,
Dominic