Here’s my code for marking a set of Tasks complete, based on the subject, and for creating a new Task:
The part that bugs me the most, is the oNS.Login. I have to put in my exchange profile and my password and yet, Outlook still asks for my password. This is why I think that the application should be an outlook plug in.
‘ Create an Outlook application.
Dim oApp As outlook.Application = New outlook.Application()
‘ Get NameSpace and Logon.
Dim oNS As outlook.NameSpace = oApp.GetNamespace("mapi")
‘ oNS.Logon("YourValidProfile", Missing.Value, False, True) ‘ TODO:
‘ mark any uncompleted tasks to complete
Dim oTasks As outlook.MAPIFolder = oNS.GetDefaultFolder(outlook.OlDefaultFolders.olFolderTasks)
‘ Get the first contact from the Contacts folder.
Dim oItems As outlook.Items = oTasks.Items
Dim oItem As outlook.TaskItem
oItems = oItems.Restrict("[Subject] = Change Infusion Set")
Dim i As Integer
For i = 1 To oItems.Count
oItem = oItems.Item(i)
If Not oItem.Complete Then oItem.MarkComplete()
oItem.Save()
Next
‘ Create a new AppointmentItem.
Dim oTask As outlook.TaskItem = oApp.CreateItem(outlook.OlItemType.olTaskItem)
‘ Set some common properties.
oTask.Subject = "Change Infusion Set"
oTask.Body = "Change Infusion Set"
‘Find number of days to add to task which is the smaller of the number of days or Total Insulin divided by TDD
Dim days As Integer = Val(txtDays.Text)
Dim StrDate As String
Dim insulinDays As Integer = Val(txtTotal.Text) / Val(txtTDD.Text)
If days > insulinDays Then
days = Int(insulinDays)
End If
StrDate = Convert.ToDateTime(Date.Today.AddDays(days))
‘Creating the Task
oTask.StartDate = StrDate
oTask.DueDate = StrDate
oTask.Complete = False
oTask.DateCompleted = Nothing
oTask.Status = outlook.OlTaskStatus.olTaskNotStarted
If reminder = True Then
oTask.ReminderSet = True
Dim StrTime As String = Convert.ToString(Me.DateTimePicker1.Value.TimeOfDay)
oTask.ReminderTime = Convert.ToDateTime(StrDate & " " & StrTime)
Else
oTask.ReminderSet = False
End If
‘ Save to Tasks
oTask.Save()
‘ Logoff.
oNS.Logoff()
‘ Clean up.
oApp = Nothing
oNS = Nothing
oTask = Nothing
‘Note I’ve lost my formatting, but I think you can get the point.
Leave a Reply