Hi,
I have searched for a code which can create a task in outlook using excel vba, but somehow it is not working. The VBA code is as:
Request to help me out on this.
I have searched for a code which can create a task in outlook using excel vba, but somehow it is not working. The VBA code is as:
Request to help me out on this.
Code:
Sub CreateTaskBatch()
' After referencing MS Outlook objects, we can use them in our script
Dim olApp As Outlook.Application
Dim olTsk As TaskItem
Dim RangeStart As Integer ' Used for looping
Dim RangeEnd As Integer ' Used for looping
Dim DueDate As Date
Dim i As Integer
' Set up the Loop Start Point
RangeStart = 3
' Obtain Range End from the Current User (Loop End iterator)
RangeEnd = InputBox("Please supply the last Row Number for your list of Engineering Requests?", _
"Range End Required")
'RangeEnd = InputBox("Please provide the end of the Range (Number)?",
'"Require Range End", 2, 100, 100, 0)
' Incorporate a Backdoor Exit for the Loop, in the event that the User enters incorrect information
' or if the User realises that they want to quit this function before it runs.
'or if nothing is type into Row 2
If (RangeEnd = -1) Or (Sheets("task").Cells(RangeStart, "A") = "") Then
olApp = Nothing ' Clear Object from System Memory
olTsk = Nothing ' Clear Task Object from Memory
Exit Sub ' Completely close down the Sub
Else
' If the user entered valid data, continue with Memory Addressing
' and Create new Object instances
Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)
' Set up the Looping function
' We will use a for..next loop since we know the starting and ending points of this loop
' which is from Row 2 (just below column headings) up to whatever row number is last occupied by data
' We set these variables at the very top of this Script
' NOW RUN THE LOOP
For i = RangeStart To RangeEnd
With olTsk
.Subject = Sheets("task").Cells(i, "c") ' I hardcode Column Access for easy maintenance later
' i is the current row number within the loop. With each iteration it will increase to (i + 1)
' Supported by the call to "Next i"
.StartDate = Sheets("task").Cells(i, "A")
' I noticed that a lot of the records have nothing n the Due Date column. This will cause programming errors
' so I check here if it is empty, and if so: I set the date to today
If Sheets("task").Cells(i, "b") = "" Then
DueDate = Now
Else
.DueDate = Sheets("task").Cells(i, "b")
End If
' Since the Engineers type/select a Text version as the Status Option, We must parse
' The physical Text. Thus, I hardcode all the text entries they can possibly make here and
' relate my text entry with an actual MS Outlook Status.Object
Select Case Sheets("Task").Cells(i, "D")
Case "Completed"
.Status = olTaskComplete
Case "In Progress"
.Status = olTaskInProgress
Case "Deferred"
.Status = olTaskDeferred
Case "Not Started"
.Status = olTaskNotStarted
Case "Waiting on someone"
.Status = olTaskWaiting
End Select
' We will do another Select Statement to establish the Importance/Priority
Select Case Sheet1.Cells(i, "E")
Case "(1) High"
.Importance = olImportanceHigh
Case "(2) Normal"
.Importance = olImportanceNormal
Case "(3) Low"
.Importance = olImportanceLow
End Select
' Percentage Completion (Real Number)
.TotalWork = 100 - Sheets("task").Cells(i, "f")
.ActualWork = Sheets("task").Cells(i, "f")
.DateCompleted = Sheets("task").Cells(i, "g")
.Owner = Sheets("task").Cells(i, "h")
.Save ' Save the Current Loop Iteration Task
.ContactNames = "nitin.d.sharma@live.com"
End With
Next i ' Move on to the Next Row
End If
Set olTsk = Nothing
Set olApp = Nothing
MsgBox "The Engineering Request Tasks have uploaded successfully!", vbInformation
End Sub