Results 1 to 12 of 12

Outlook.Application / Run-time error '287' when Outlook isn't running

  1. #1
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114

    Outlook.Application / Run-time error '287' when Outlook isn't running

    I am trying to use Access to start a new email message in Outlook. The code below works fine only if Outlook is already open when I run the code. If Outlook is not open, I get the following error:



    Run-time error '287':
    Application-defined or object-defined error.

    The following line is highlighted by the debugger: Set objOutlookRecip = objOutlookMsg.Recipients.Add(rsRecip("Email")).

    Any suggestions?
    Any other ideas about how to improve or simplify the code would be appreciated.
    Thanks

    ps. Office 2010.

    Code:
    Private Sub EmailParticipants_Click()
    
    Dim db As dao.Database
    Dim rsRecip As dao.Recordset
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    
    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")
    Set db = CurrentDb
    
    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    
    ' Open the recordset so you can loop through it.
    Set rsRecip = db.OpenRecordset("qryParticipants")
    
    Do Until rsRecip.EOF
    
    ' Add recipients
    
    Set objOutlookRecip = objOutlookMsg.Recipients.Add(rsRecip("Email"))
    objOutlookRecip.Type = olBCC
    
    rsRecip.MoveNext
    
    Loop
    
    With objOutlookMsg
    .To = "test@test.tt"
    .Subject = "Program Information"
    .Body = "Thank you for registering. Please see the attached file for more information."
    .Attachments.Add ("C:\program.xlsx")
    End With
    
    objOutlookMsg.Display
    
    'Cleanup
    ExitProc:
    If Not rsRecip Is Nothing Then
    rsRecip.Close: Set rsRecip = Nothing
    End If
    Set db = Nothing
    Exit Sub
    ProcError:
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Error in SendMessage Procedure..."
    Resume ExitProc
    Resume
    
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,384
    I use code almost identical to yours and it runs fine if Outlook is open or closed. However, my procedure is to a single address and not building a Recipients object. Never seen it used before. Do a test without it and see what happens.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Quote Originally Posted by June7 View Post
    I use code almost identical to yours and it runs fine if Outlook is open or closed. However, my procedure is to a single address and not building a Recipients object. Never seen it used before. Do a test without it and see what happens.
    Thank you for your reply. The recordset recipients object works fine when Outlook is already open.
    Is it possible that this feature itself will not work because Outlook is not open? I thought that the problem could be with the CreateObject or CreateItem.

    What do you think?
    Thanks again.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,384
    Don't know, that's why I suggested testing without it and with Outlook closed. Does it still error?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Sorry for the delay. You were right. If I remove
    Code:
    Set objOutlookRecip = objOutlookMsg.Recipients.Add(rsRecip("Email")) 
    objOutlookRecip.Type = olBCC
    Outlook opens and a new message is created using
    Code:
    With objOutlookMsg 
    .To = "test@test.tt" 
    .Subject = "Program Information" 
    .Body = "Thank you for registering. Please see the attached file for more information." 
    .Attachments.Add ("C:\program.xlsx") 
    End With
    I tried the following change:
    Code:
    Set objOutlookRecip = objOutlookMsg.Recipients.Add("test@test.com") 
    objOutlookRecip.Type = olBCC
    but that didn't work. I am still getting the error message mentioned above.
    Any ideas?
    Thanks

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,384
    Don't use Recipients. Just use a variable to build a string of the addressess then set the .Bcc property with the variable.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Thank you. I am pretty new to this, and am not sure how to build the recipients list using a variable.
    I tried the following, but that only added the first address in the recordset:

    Code:
    Set rsRecip = db.OpenRecordset("qryParticipants")  
    
    Do Until rsRecip.EOF    
    
       With objOutlookMsg
      .BCC = (rsRecip("Email"))
       End With  
    
    rsRecip.MoveNext  
    Loop
    Do you suggest not using a recordset at all or using a recordset to concatenate addresses in a variable and then adding a Bcc line in my With objOutlookMsg where I currently have the To, Subject, Body, and Attachments?

    Thanks again.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,384
    Use the recordset to build the string. Loop through as you did before but instead of append to Recipients object, concatenate string.

    Dim strBCC as String
    Do Until rsRecip.EOF
    strBCC = strBCC & rsRecip!Addressfield & ";"
    rsRecip.MoveNext
    Loop
    strBCC = Left(strBCC, Len(strBCC)-1) 'drops the lone ; at end of string
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    alpinegroove is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2011
    Posts
    114
    Perfect, thank you. That works.
    Do you have any idea why the Recipients method doesn't work even though the code initiates the Outlook session and starts a new message?

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,384
    No, I've never seen that method before this thread. Maybe it would work if code actually fully opened Outlook, instead of just creating object in VBA.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Bawtry_support is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    1
    This thread is a bit old but for new readers I hope this may be useful:

    If using Outlook automation to generate an appointment (as distinct from a vanilla email) then the Recipients.add method is required to add the attendees. I believe the cc list (given as the solution above) is available for email but not for appointments.

    Code:
      Set appOutlook = New outlook.Application
      Set apptItem = appOutlook.CreateItem(outlook.OlItemType.olAppointmentItem)
      With apptItem
        .MeetingStatus = outlook.OlMeetingStatus.olMeeting
        .Subject = Action & ".    Appointment with Patient: " & i.PatientNo
        .Body = msg
        .Start = i.startTimeAndDate
        .End = i.endTimeAndDate
        .Location = Me.locationName
        .display
      End With
    ... and then add each Recipient
    Code:
          Set recipient = apptItem.Recipients.Add(emailAddress)
          recipient.Type = outlook.OlMeetingRecipientType.olRequired
    But there is a snag which causes exception 287 to be thrown. Microsoft Outlook requires confirmation by the user that it is OK for code to add an email address. If Outlook is not currently visible this degenerates into the generic error 287. By inserting the line .display the programmer can see the dialog Outlook displays as each Recipient is added.

    There are some circumstances where this does not happen: for example if Outlook detects an anti-virus product is active on the users system.

    This is documented at http://www.outlookcode.com/article.aspx?ID=52

  12. #12
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    Or, just teach your users to go into their Outlook settings (one time per user, per machine) and set Allow Programmatic Access to 'always' or 'yes'. Then you can program the Outlook object model all day long and you are good to go.

    Or, make Outlook application visible first, in which case Outlook's security popup will be usually one time for a duration of time.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Outlook error message using SendObject method
    By Juan23 in forum Programming
    Replies: 3
    Last Post: 07-10-2013, 12:03 PM
  2. Assigning Outlook task from shared Outlook mailbox
    By Remster in forum Programming
    Replies: 2
    Last Post: 11-16-2011, 04:38 AM
  3. email with outlook.application
    By JJCHCK in forum Access
    Replies: 5
    Last Post: 08-25-2011, 06:19 AM
  4. Error opening Outlook from Access
    By Toots in forum Programming
    Replies: 2
    Last Post: 03-13-2011, 06:00 PM
  5. Sending Outlook E-mail - Run-time error '429'
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 03-01-2011, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums