J'ai le code suivant pour envoyer des emails à des personnes dont les informations sont extraites d'une base de données:
Dim oMsg As Outlook._MailItem
Dim objOL As Outlook.Application
objOL = New Outlook.Application()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
con = New OleDbConnection("provider=SQLOLEDB;data source=pc;initial catalog=DB1;integrated security=SSPI")
cmd = New OleDbCommand("select column1, column2, column3, column4 from table1 where <condition>", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
oMsg.Subject = "Subject"
oMsg.Body = "Hello " & r.Item(0) & vbLf & vbCr & "How are " & r.Item(1) & " and" & r.Item(2) & vbLf & vbCr & " ? "
oMsg.To = r.Item(3).ToString
oMsg.Send()
End While
con.Close()
oMsg = Nothing
objOL = Nothing
Le problème est qu’après l’envoi du premier courrier électronique, une erreur non gérée COMException s’est produite, indiquant que l’élément a été déplacé ou supprimé. Qu'est-ce qui ne va pas ici?
Réponses:
1 pour la réponse № 1Je crois que vous avez besoin d’un nouveau message Outlook à chaque boucle:
Dim oMsg As Outlook._MailItem
Dim objOL As Outlook.Application
objOL = New Outlook.Application()
con = New OleDbConnection("provider=SQLOLEDB;data source=pc;initial catalog=DB1;integrated security=SSPI")
cmd = New OleDbCommand("select column1, column2, column3, column4 from table1 where <condition>", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "Subject"
oMsg.Body = "Hello " & r.Item(0) & vbLf & vbCr & "How are " & r.Item(1) & " and" & r.Item(2) & vbLf & vbCr & " ? "
oMsg.To = r.Item(3).ToString
oMsg.Send()
oMsg = Nothing
End While
con.Close()
objOL = Nothing