Create outlook task in non-default folder

Art Eby

New Member
Joined
Nov 17, 2016
Messages
3
Hello

I am trying to create outlook tasks in a non-default folder with windows 7 excel 2016. I found some code and adapted it a bit for making tasks, but i get "run-time error '438' object doesn't support this property or method" on this line of the "GetFolderPath" function:

Set oFolder = Application.Session.Folders.Item(FoldersArray(0))

I have the outlook object library reference set. Any help would be appreciated.

Code:
[INDENT]
Option Explicit
Public Sub CreateOutlookAppt()
   Sheets("Sheet1").Select
   ' On Error GoTo Err_Execute
      
    Dim olApp As Outlook.Application
    Dim olAppt As Outlook.TaskItem
    Dim blnCreated As Boolean
    Dim olNs As Outlook.Namespace
    Dim CalFolder As Outlook.MAPIFolder
    Dim arrCal As Outlook.MAPIFolder
    Dim tzStart As TimeZone, tzEnd As TimeZone
  
    Dim i As Long
          
    On Error Resume Next
  
 Set olApp = Outlook.Application
 Set tzStart = olApp.TimeZones.Item("Eastern Standard Time")
 Set tzEnd = olApp.TimeZones.Item("UTC")
 
    If olApp Is Nothing Then
        Set olApp = Outlook.Application
         blnCreated = True
        Err.Clear
    Else
        blnCreated = False
    End If
      
    On Error GoTo 0
      
    Set olNs = olApp.GetNamespace("MAPI")
    Set CalFolder = GetFolderPath("\\arthur@sportingvalleyfeeds.com\Tasks")

    i = 1
    Do Until Trim(Cells(i, 1).Value) = ""
     
    Set olAppt = CalFolder.Items.Add(olTaskItem)
            
    With olAppt
      
    'Define calendar item properties
    .Subject = Cells(i, 2)
    .StartDate = Cells(i, 3)
    .DueDate = Cells(i, 4)
    .ReminderTime = 15
    .Status = olTaskNotStarted
    .ReminderSet = True
    .Body = Cells(i, 2)
    .Save
      
    End With
        
        i = i + 1
        Loop
    Set olAppt = Nothing
    Set olApp = Nothing
      
    Exit Sub
      
Err_Execute:
    MsgBox "An error occurred - Exporting items to Calendar."
      
End Sub
[/INDENT]

Here is the function:

Code:
[INDENT]
Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
   
    Dim FoldersArray As Variant
    Dim i As Integer
    Dim oFolder As Outlook.Folder
    
   
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
    If Not oFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = oFolder.Folders
            Set oFolder = SubFolders.Item(FoldersArray(i))
            If oFolder Is Nothing Then
                Set GetFolderPath = Nothing
            End If
        Next
    End If
    'Return the oFolder
    Set GetFolderPath = oFolder
    Exit Function
        
GetFolderPath_Error:
    Set GetFolderPath = Nothing
    Exit Function
End Function
[/INDENT]
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,225,156
Messages
6,183,242
Members
453,152
Latest member
ChrisMd

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