Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not all recipients added to Outlook MailItem

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
NL
I am using the following to try and add recipients to an Outlook MailItem:

Code:
Dim objApp As outlook.Application
Dim objMail As MailItem
Dim objBCC As outlook.Recipient

On Error GoTo Err_Mail

Set objApp = CreateObject("Outlook.application")
Set objMail = objApp.CreateItem(olMailItem)
Set objBCC = objMail.Recipients.Add(pubAuditorEmail)
For i = 1 To 5
  If Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(0) <> pubAuditor_i Then
    Set objBCC = objMail.Recipients.Add(Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(2))
  End If
Next i
objBCC.Type = olBCC
....

I find however, that objBCC then only contains the last recipient added in the For loop (all the addresses are valid). What am I doing wrong?
 
First it looks to me like you want objBCC to be a collection of recipients not a single recipient. Also inside the loop you keep resetting it. I think you want
Code:
Dim objApp As outlook.Application
Dim objMail As MailItem
[b]Dim objBCC As outlook.Recipient[COLOR=#EF2929]s[/color][/b]
On Error GoTo Err_Mail
Set objApp = CreateObject("Outlook.application")
Set objMail = objApp.CreateItem(olMailItem)
Set ObjBcc = ObjApp.recipients
objBCC.Type = olBCC
objBCC.Recipients.Add(pubAuditorEmail)
For i = 1 To 5
  If Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(0) <> pubAuditor_i Then
    objBCC.Recipients.Add(Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(2))
  End If
Next i
 
Thank you, MajP, but this gives a Type mismatch error at the first 'Set objBCC...'. I have subsequently discovered that the solution seems to be to declare the type of each recipient immediately after adding it:

Code:
Dim objApp As Outlook.Application
Dim objMail As MailItem
Dim objBCC As Outlook.Recipient
On Error GoTo Err_Mail
Set objApp = CreateObject("Outlook.application")
Set objMail = objApp.CreateItem(olMailItem)
[highlight #EDD400]Set objBCC = objMail.Recipients.Add(pubAuditorEmail)
objBCC.Type = olBCC[/highlight]
For i = 1 To 5
If Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(0) <> pubAuditor_i Then
  [highlight #EDD400]Set objBCC = objMail.Recipients.Add(Me.Controls("Subform" & Format(i, "0"))!keuAuditor.Column(2))
  objBCC.Type = olBCC[/highlight]
End If
Next i
objMail.Subject = MijnSubject
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top