Outlook 2007 Recipients.Add problem


A developer has created a job management database that interfaces with Outlook 2007 to eMail job tenders to contractors. Under Office 2003 it works fine, but under Office 2007 it fails.

The system has a valid and up-to-date anti-virus solution, and the programmer is using "late binding". Actual code being used is listed below, with the error occuring at the line:

"Set objOutlookRecip = objOutlookMsg.Recipients.Add(Address(i))"

We're stumped on this - any ideas would be very much appreciated.

Public Sub SendMailPlus(Address() As String, Subject As String, message As String, mailType As Integer, _
   importance As Integer, AttachmentPath() As String)
On Error GoTo Err
   Dim i As Integer
   ' Converted to late binding for compatibility between different versions of Outlook
   Dim objOutlook As Object         ' was Outlook.Application
   Dim objOutlookMsg As Object      ' was Outlook.MailItem
   Dim objOutlookRecip As Object    ' was Outlook.Recipient
   Dim objOutlookAttach As Object   ' was Outlook.Attachment
   ' Create the Outlook session.
   Set objOutlook = CreateObject("Outlook.Application")
   ' Create the message.
   Set objOutlookMsg = objOutlook.CreateItem(0)    ' was 0 = olMailItem
   ' Add BCC recipients
   For i = 1 To maxEmails
      If Len(Address(i)) > 0 Then
         Set objOutlookRecip = objOutlookMsg.Recipients.Add(Address(i))
         objOutlookRecip.Type = mailType
      End If
   Next i
   ' Message parameters
   objOutlookMsg.Subject = Subject
   objOutlookMsg.Body = message & vbCrLf & vbCrLf
   objOutlookMsg.importance = importance
   objOutlookMsg.ReadReceiptRequested = True
   ' Add primary attachment
   Set objOutlookAttach = objOutlookMsg.Attachments.Add(AttachmentPath(0))
   ' Add secondary attachments
   For i = 1 To maxFiles - 1
      If Len(AttachmentPath(i)) > 0 Then
         If FileExists(AttachmentPath(i)) Then
            Set objOutlookAttach = objOutlookMsg.Attachments.Add(AttachmentPath(i))
         End If
      End If
   Next i
   ' Send email
   ' Cleanup
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
   Exit Sub
   Call MsgBox("There was an error creating the email messages", vbCritical, kError)
End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hello The_Maverick,

When you say it fails, what is the exact error message?  You will need to turn off the error handler to get it...


The_MaverickAuthor Commented:
Sorry about the delay in getting back to you Patrick. I'll need to get the developer to talk me through turning off the error handler next time I'm on site - which will probably be Monday now.
Patrick MatthewsCommented:

That's easy--just comment out "On Error GoTo Err"



The_MaverickAuthor Commented:
"That's easy--just comment out "On Error GoTo Err""

Easy for you :)
The_MaverickAuthor Commented:
Hi Patrick (and others),

Problem solved by developer ...

The developer discovered that if Outlook 2007 is already running when the eMail is sent then it works properly, whereas if Outlook ISN'T already running it fails. According to the developer this is a Microsoft "feature" to prevent malware from sending eMails via outlook without the users knowledge.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.