Convert to Late Binding for Excel VB to Outlook task list

mcparch1

New Member
Joined
Feb 28, 2014
Messages
2
Here is the code I found and slightly altered. It gets a run-time error on the bolded line below. It worked prior to me trying to convert but now I need to make it compatible with other versions of Outlook. Please advise.

Sub CreateTask()
Dim OLApp As Object
Dim olName As Object
Dim olFolder As Object
Dim olTasks As Object
Dim olNewTask As Object
Dim strSubject As String
Dim strDate As String
Dim strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim LR As Long
Dim i As Long
Set ws = Worksheets("Entry Form") 'sheet where dates are
LR = ws.Range("E4").End(xlDown).Row 'get row for last cell in column D with value
Set OLApp = CreateObject("Outlook.Application")
Set olName = OLApp.GetNamespace("MAPI")
Set olFolder = olName.GetDefaultFolder(olFolderTasks)
Set olTasks = olFolder.Items
For i = 5 To LR 'assuming the rows have headers, so loop starts on row 2
strSubject = ws.Range("E" & i) 'takes subject from column D
strDate = ws.Range("B" & i) 'takes date from column C
strBody = ws.Range("D" & i) 'takes text from column E and adds it as Body
reminderdate = ws.Range("A" & i) 'Takes date from column D and enters it as the reminder date
Set olNewTask = olTasks.Add(olTaskItem)
'delete task if it exists
'an error is generated if task doesn't exist
On Error Resume Next
olTasks.Item (strSubject)
If Err.Number = 0 Then
olTasks.Item(strSubject).Delete
End If
On Error GoTo 0
'create new task
If Range("M" & i) = Environ("Username") Then
With olNewTask
.Subject = strSubject
.Status = olTaskInProgress
.Importance = olImportanceNormal
.StartDate = DateValue(strDate)
.DueDate = ws.Range("A" & i)
.Body = strBody
.ReminderSet = True
.ReminderTime = reminderdate
.Save
End With
End If
Next i
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
In future please use code tags when posting code.

When using late bindings you cannot use long references like olFolderTasks it needs to be the enumeration.

Code:
Set olFolder = olName.GetDefaultFolder(13)

You will also need to change olTaskItem to 3, olTaskInProgress to 1 and olImportanceNormal to 1.

BTW to quickly get the enumeration open outlook VBE and in the immediate window type ? and the word you want.

?olImportanceNormal
 
Upvote 0
Declare the Outlook constants you use.
Code:
Const olFolderTasks = 13
Const olImportanceNormal = 1
Const olTaskItem = 3
Const olTaskInProgress = 1
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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