title: Excel to create task in Outlook
body: <p>I am relatively new to VBA and so I'm sure this is a basic mistake that I am making!</p>
  
  <p>A1 contains valid date, A2 contains task information, A3 contains number of days before A1 date to trigger task reminder.</p>
  
  <p>The problem is when I put =addtotasks(A1,A2,A3) </p>
  
  <p>It just comes up with #NAME?</p>
  
  <p>I have enabled the Microsoft Outlook 14.0 Object Library in the references.</p>
  
  <p>I am based in the UK telling you for dates purposes.</p>
  
  <p>This is the following code. I have included some extra code for setting a remainder only on a business day.</p>
  
  <pre><code>'Function NextBusinessDay(dateFrom As Date, _
    Optional daysAhead As Long = 1) As Date
   Dim currentDate As Date
   Dim nextDate As Date
  
  ' convert neg to pos
   If daysAhead &lt; 0 Then
    daysAhead = Abs(daysAhead)
  End If
  
  ' determine next date
   currentDate = dateFrom
   nextDate = DateAdd("d", daysAhead, currentDate)
  
   ' is next date a weekend day?
    Select Case Weekday(nextDate, vbUseSystemDayOfWeek)
  Case vbSunday
  nextDate = DateAdd("d", 1, nextDate)
   Case vbSaturday
   nextDate = DateAdd("d", 2, nextDate)
    End Select
  
    NextBusinessDay = CDate(Int(nextDate))
  
  
  End Function
  
  Dim bWeStartedOutlook As Boolean
  
  Function AddToTasks(strDate As String, strText As String, DaysOut As Integer) As Boolean
  ' Adds a task reminder to Outlook Tasks a specific number of days before the date specified
  ' Returns TRUE if successful ' Will not trigger OMG because no protected properties are accessed
  '
  ' Usage:
  ' =AddToTasks("12/31/2008", "Something to remember", 30)
  ' or:
  ' =AddToTasks(A1, A2, A3)
  ' where A1 contains valid date, A2 contains task information, A3 contains number of days before A1 date to trigger task reminder '
  ' can also be used in VBA :
  'If AddToTasks("12/31/2008", "Christmas shopping", 30) Then
  '  MsgBox "ok!"
  'End If
  Dim intDaysBack As Integer
  Dim dteDate As Date
  Dim olApp As Object 'Outlook.Application
  Dim objTask As Object ' Outlook.TaskItem
  ' make sure all fields were filled in
  If (Not IsDate(strDate)) Or (strText = "") Or (DaysOut &lt;= 0) Then
  AddToTasks = False
  GoTo ExitProc
  End If
  ' We want the task reminder a certain number of days BEFORE the due date
  ' ex: if DaysOut = 120, then we want the due date to be -120 before the date specified
  ' we need to pass -120 to the NextBusinessDay function, so to go from 120 to -120,
  ' we subtract double the number (240) from the number provided (120).
  ' 120 - (120 * 2); 120 - 240 = -120
  intDaysBack = DaysOut - (DaysOut * 2)
  
  dteDate = NextBusinessDay(CDate(strDate), intDaysBack)
  
  On Error Resume Next
     Set olApp = GetOutlookApp
  On Error GoTo 0
  
  If Not olApp Is Nothing Then
     Set objTask = olApp.CreateItem(3)   ' task item
  
     With objTask
      .StartDate = dteDate
      .Subject = strText &amp; ", due on: " &amp; strDate
      .ReminderSet = True
      .Save
     End With
  
  Else
     AddToTasks = False
     GoTo ExitProc
  End If
  
     ' if we got this far, it must have worked
  AddToTasks = True
  
  ExitProc:
  If bWeStartedOutlook Then
     olApp.Quit
  End If
  Set olApp = Nothing
  Set objTask = Nothing
  End Function
  
  Function GetOutlookApp() As Object
  
  On Error Resume Next
     Set GetOutlookApp = GetObject(, "Outlook.Application")
     If Err.Number &lt;&gt; 0 Then
      Set GetOutlookApp = CreateObject("Outlook.Application")
      bWeStartedOutlook = True
     End If
  On Error GoTo 0
  
  End Function
  </code></pre>
  
  <hr>
  
  <p>I have taken this code from this website, by the way: 
  <a href="http://www.jpsoftwaretech.com/get-previous-business-day-in-vba/" rel="nofollow">http://www.jpsoftwaretech.com/get-previous-business-day-in-vba/</a></p>