Recipients.Add method causing Error 287

Excel1user

New Member
Joined
May 20, 2013
Messages
6
I am writing vba code to create Outlook tasks for various employees from an Excel spreadsheet. The spreadsheet contains a list of reports under review and who is assigned to review the task.
I want the vba code to create an Outlook task for the individual assigned to review the report. I have found a number of websites with example code of how to do this, but everytime I have tried to use the code I get the same error on the "Recipients.Add" line. The error is vba "Run-time error '287': Application-defined or object-defined error".
Here is the code I'm using: (It is interesting to note that I have written code that successfully writes an e-mail, but that code uses the ".To" method, instead of the "Recipients.Add" method. Unfortunately the ".To" method is not available for Task Items.)
Rich (BB code):
Option Explicit
Sub createtask()
    Dim olApp As Outlook.Application, olNs As Outlook.Namespace, olTask As Outlook.TaskItem
    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
        olNs.Logon
    Set olTask = olApp.CreateItem(olTaskItem)
    olTask.Assign
    olTask.Subject = "Test"
    olTask.Body = "Testing macro"
    olTask.Recipients.Add ("myemail@myemail.com")
    olTask.Display
End Sub
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
That code works fine for me.

Is that the actual code that's causing an error?
 
Upvote 0
Yes the above code is exactly the code I am trying to use. It gives me the error on the Recipients.Add method. I can create a task just fine, but if I try to use the Recipients.Add method then it fails every time.
I tried the code below in Outlook vba and it worked perfectly, so it appears that there is an issue with Excel vba using the Outlook Recipients.Add method. Is anyone aware of anything that might prevent using the Recipients.Add method in Excel vba, but not Outlook vba?

Rich (BB code):
Public Sub CreateTask1()
    Dim olTask As TaskItem
    Set olTask = CreateItem(olTaskItem)
    olTask.Assign
    olTask.Subject = "Test"
    olTask.Body = "Testing macro"
    olTask.Recipients.Add ("myemail@myemail.com")
    olTask.Display
End Sub
 
Last edited by a moderator:
Upvote 0
Is Outlook open when you get the error?
 
Upvote 0
Thanks for the help. I have found a temporary work-around using the "SendKeys" method. Although I would like to find a better solution because SendKeys is not good practice, and is prone to malfunction if a different application is activated for some reason.

By the way, I am using Windows 7 and Office 2010. I also tested this code on a co-workers computer who still had Office 2007 and I got the same error.
 
Upvote 0
Why are you using Recipients.Add when there's only one recipient?

Can't you use To?
 
Upvote 0
Couldn't tell you why you're getting that error or why SendKeys would fix it, perhaps you're missing a windows update which seems to be the cause of a lot of these random errors.

Out of interest does this minor alteration work (again there's no reason it should)

Code:
Sub createtask()
    Dim olApp As Outlook.Application, olNs As Outlook.Namespace, olTask As Outlook.TaskItem, olRec As Outlook.Recipient
    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
        olNs.Logon
    Set olTask = olApp.CreateItem(olTaskItem)
    olTask.Assign
    olTask.Subject = "Test"
    olTask.Body = "Testing macro"
    Set olRec = olTask.Recipients.Add("[EMAIL="myemail@myemail.com"]myemail@myemail.com[/EMAIL]")
    olRec.Resolve
    olTask.Display
End Sub

On a seperate note you might want to consider using a With Statement for olTask to both tidy up your code and avoid having to reference olTask for each property
 
Upvote 0
Thanks for the attempt, but no that failed as well with the same error on the line
Rich (BB code):
Set olRec = olTask.Recipients.Add("myemail@myemail.com")
Just for completeness, here is the code that works using SendKeys as a work-around.
Rich (BB code):
Sub createtask()
    Dim olApp As Outlook.Application, olNs As Outlook.Namespace, olTask As Outlook.TaskItem
    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
        olNs.Logon
    Set olTask = olApp.CreateItem(olTaskItem)
    olTask.Assign
    olTask.Subject = "Test"
    olTask.Body = "Testing macro"
    olTask.Display
    SendKeys "myemail@myemail.com"
End Sub
 
Upvote 0
Norie,
Unfortunately the .To method is only available for composing an e-mail. It is not an option for creating a TaskItem.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top