Hey guys, great forum, I've been using it a lot while doing this work in Excel.
I am writing code to have Tasks in Outlook created by a huge list of tasks in an Excel document. I want to skip all the blank rows and rows of assignments that are already completed. So I am trying to make it so if the Due Date column (Column E) is NOT blank and the Actual Completion Date column (Column E) IS blank then it should create an outlook task. I have it so it is able to create outlook tasks but I am running into two problems.
1) In the section right under "For i = 9 to 25000" where it is supposed to ignore the row based on the above mentioned criteria it gives me the error "Compile error: Next without For" on the line that says Next i
2) In the section where I am trying to assign a due date near the bottom it gives me an Error 13 for the line .DueDate = DateValue(strDate)
Here is the code
I am writing code to have Tasks in Outlook created by a huge list of tasks in an Excel document. I want to skip all the blank rows and rows of assignments that are already completed. So I am trying to make it so if the Due Date column (Column E) is NOT blank and the Actual Completion Date column (Column E) IS blank then it should create an outlook task. I have it so it is able to create outlook tasks but I am running into two problems.
1) In the section right under "For i = 9 to 25000" where it is supposed to ignore the row based on the above mentioned criteria it gives me the error "Compile error: Next without For" on the line that says Next i
2) In the section where I am trying to assign a due date near the bottom it gives me an Error 13 for the line .DueDate = DateValue(strDate)
Here is the code
Code:
Sub CheckBinding()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
MsgBox olApp.Name
End Sub
Sub CreateTask()
Dim olApp As New Outlook.Application
Dim olName As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olTasks As Outlook.Items
Dim olNewTask As Outlook.TaskItem
Dim strSubject As String
Dim strDate As String
Dim strBody As String
Dim reminderdate As String
Dim ws As Worksheet
Dim i As Long
Dim DueCheck As Range
Set ws = Worksheets("KPI") 'sheet where dates are
Set olName = olApp.GetNamespace("MAPI")
Set olFolder = olName.GetDefaultFolder(olFolderTasks)
Set olTasks = olFolder.Items
LR = ws.Range("C9").End(xlDown).Row 'get row for last cell in column D with value
For i = 9 To 25000 'assuming the rows have headers, so loop starts on row 2
If Not IsEmpty(ws.Range("E", i)) And ws.Range("J", i) = "" Then
Next i
Else
strSubject = ws.Range("C" & i) 'takes subject from column c
strDate = ws.Range("E" & i) 'takes date from column e
strBody = ws.Range("C8") & Chr(10) & ws.Range("C" & i) & Chr(10) & Chr(10) & ws.Range("D8") & Chr(10) & ws.Range("D" & i) & Chr(10) & Chr(10) & ws.Range("F8") & Chr(10) & ws.Range("F" & i) & Chr(10) & Chr(10) & ws.Range("G8") & Chr(10) & ws.Range("G" & i) & Chr(10) & Chr(10) & ws.Range("K7") & Chr(10) & ws.Range("K" & i)
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
With olNewTaskthat
Subject = strSubject
.Importance = olImportanceNormal
.DueDate = DateValue(strDate)
.Body = strBody
.ReminderSet = True
.Save
End With
End If
Next i
End Sub
Sub CreateOutlookTask()
End Sub