473,499 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code not working

72 New Member
Hi,
this code is fine but it doesnot loop. It only send one email (the top one on the list of the table). How to fix it?
thanks in advance

Private Sub Command0_Click()

Dim db As Database
Dim rs As DAO.Recordset
'Dont't forget to reference the Outlook Object Library
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Email FROM Table1")


Do Until rs.EOF
On Error GoTo ErrorHandler

With objEmail
.To = rs!Email
.Subject = "Happy Holidays"
.HTMLBody = "Greetings from CD <br><br>"
.Attachments.Add "P:\Greetings.jpg", olByValue, , "Stuff"


.Send
End With
ErrorHandler:

Resume Next

rs.MoveNext

Loop


End Sub
Dec 13 '07 #1
3 1895
ADezii
8,834 Recognized Expert Expert
Hi,
this code is fine but it doesnot loop. It only send one email (the top one on the list of the table). How to fix it?
thanks in advance

Private Sub Command0_Click()

Dim db As Database
Dim rs As DAO.Recordset
'Dont't forget to reference the Outlook Object Library
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Email FROM Table1")


Do Until rs.EOF
On Error GoTo ErrorHandler

With objEmail
.To = rs!Email
.Subject = "Happy Holidays"
.HTMLBody = "Greetings from CD <br><br>"
.Attachments.Add "P:\Greetings.jpg", olByValue, , "Stuff"


.Send
End With
ErrorHandler:

Resume Next

rs.MoveNext

Loop


End Sub
Try this revised code and let me know how it works out:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2. On Error GoTo ErrorHandler
  3.  
  4. Dim db As DAO.Database
  5. Dim rs As DAO.Recordset
  6.  
  7. 'Dont't forget to reference the Outlook Object Library
  8. Dim objOutlook As Outlook.Application
  9. Dim objEmail As Outlook.MailItem
  10.  
  11. Set objOutlook = CreateObject("Outlook.Application")
  12. Set objEmail = objOutlook.CreateItem(olMailItem)
  13.  
  14. Set db = CurrentDb
  15. Set rs = db.OpenRecordset("SELECT Email FROM Table1")
  16.  
  17. Do Until rs.EOF
  18.   With objEmail
  19.     .To = rs!Email
  20.     .Subject = "Happy Holidays"
  21.     .HTMLBody = "Greetings from CD <br><br>"
  22.     .Attachments.Add "P:\Greetings.jpg", olByValue, , "Stuff"
  23.       .Send
  24.   End With
  25.     rs.MoveNext
  26. Loop
  27.  
  28. Exit_Handler:
  29.   Exit Sub
  30.  
  31. ErrorHandler:
  32.   MsgBox Err.Description, vbExclamation, "Error in E-Mail Transmission"
  33.   Resume Exit_Handler
  34. End Sub
Dec 14 '07 #2
ielamrani
72 New Member
Perfect. Thank you so much and have a happy Holidays
Dec 14 '07 #3
ielamrani
72 New Member
Sorry, but it's still sending to one email only. also, I want to know if it's possible not to show the recipient's email address. I am sending this to 206 clients and we don't want to show everyone address.
thanks
Dec 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

171
7579
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
2668
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
0
1571
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.