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
 

BrianMH

Well-known Member
Joined
Mar 26, 2008
Messages
1,490
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Declare the Outlook constants you use.
Code:
Const olFolderTasks = 13
Const olImportanceNormal = 1
Const olTaskItem = 3
Const olTaskInProgress = 1
 

mcparch1

New Member
Joined
Feb 28, 2014
Messages
2
Thank you. That worked great. I will use code tags going forward. Thanks again.
 

Forum statistics

Threads
1,081,530
Messages
5,359,349
Members
400,524
Latest member
Excelbat

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top