Share via


Tasks

Outlook Developer Reference
Tasks

You can link a Task to a new or existing Account, Business Contact, or Opportunity object.

The following C# and Visual Basic for Applications (VBA) examples show how to create a new task and link it to a new Account object.

  private void CreateTask()
        {
        Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
        Outlook.Application olApp = (Outlook.Application)_app;
        Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
        Outlook.Folders folders = olNameSpace.Session.Folders;
        Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];

        Outlook.Folder accountsFolder = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];
        Outlook.ContactItem newAccount = (Outlook.ContactItem)accountsFolder.Items.Add("IPM.Contact.BCM.Account");
        Outlook.Folder historyFolder = (Outlook.Folder)bcmRootFolder.Folders["Communication History"];

        newAccount.FullName = "Wide World Importers";
        newAccount.FileAs = "WWImporters";
        newAccount.Save();
        string accountEntryID = newAccount.EntryID;

        Outlook.TaskItem taskItem = (Outlook.TaskItem)historyFolder.Items.Add("IPM.Task");
        taskItem.Subject = "Discussion with Sales Manager";
        taskItem.Body = "As per the discussion on 23-Jun-2005, agreed to give a 15% discount on our products";
        taskItem.StartDate = System.DateTime.Parse("3/20/2006");
        taskItem.DueDate = System.DateTime.Parse("4/20/2006");
        taskItem.Save();

        Outlook.JournalItem oHistoryItem = (Outlook.JournalItem)historyFolder.Items.Add("IPM.Activity.BCM");
        oHistoryItem.Type = "Task";
        oHistoryItem.Subject = taskItem.Subject;
        oHistoryItem.Start = taskItem.StartDate;
        oHistoryItem.End = taskItem.DueDate;

        //If the Outlook Task item is deleted, the information is preserved in the body of the Business Activity
        oHistoryItem.Body = "StartDate: " + taskItem.StartDate + "\n" + "DueDate: " + taskItem.DueDate;

        if (oHistoryItem.UserProperties["Parent Entity EntryID"] == null)
        {
            Outlook.UserProperty userProp = oHistoryItem.UserProperties.Add("Parent Entity EntryID", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = accountEntryID;
        }

        if (oHistoryItem.UserProperties["LinkToOriginal"] == null)
        {
            Outlook.UserProperty userProp = oHistoryItem.UserProperties.Add("LinkToOriginal", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
            userProp.Value = taskItem.EntryID;
        }

        oHistoryItem.Save();

    }
  Sub CreateTaskWithAccount()

Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Dim bcmRootFolder As Outlook.Folder Dim olFolders As Outlook.Folders Dim bcmAccountsFldr As Outlook.Folder Dim bcmHistoryFolder As Outlook.Folder Dim newAcct As Outlook.ContactItem Dim newHistoryTaskItem As Outlook.JournalItem Dim newTaskItem As Outlook.TaskItem Dim userProp As Outlook.UserProperty Dim str As String

Set olApp = CreateObject("Outlook.Application") Set objNS = olApp.GetNamespace("MAPI") Set olFolders = objNS.Session.Folders Set bcmRootFolder = olFolders("Business Contact Manager") Set bcmAccountsFldr = bcmRootFolder.Folders("Accounts") Set bcmHistoryFolder = bcmRootFolder.Folders("Communication History")

Set newAcct = bcmAccountsFldr.Items.Add("IPM.Contact.BCM.Account") newAcct.FullName = "Wide World Importers" newAcct.FileAs = "Wide World Importers" newAcct.Email1Address = "someone@example.com" newAcct.Save

Set newTaskItem = olApp.CreateItem(olTaskItem) newTaskItem.Subject = "Prepare Agenda For Meeting" newTaskItem.StartDate = "03/02/06" newTaskItem.DueDate = "03/03/06" newTaskItem.Save

Set newHistoryTaskItem = bcmHistoryFolder.Items.Add("IPM.Activity.BCM") newHistoryTaskItem.Subject = newTaskItem.Subject newHistoryTaskItem.Type = "Task" newHistoryTaskItem.Start = newTaskItem.StartDate newHistoryTaskItem.End = newTaskItem.DueDate

'The reason we are concatenating StartDate and DueDate and assigning them to the newHistoryTaskItem body is as follows: 'In case the Outlook Task gets deleted, this information is preserved in the body of the Business Activity newHistoryTaskItem.Body = "StartDate: " & newTaskItem.StartDate & vbNewLine & "DueDate: " & newTaskItem.DueDate

If (newHistoryTaskItem.UserProperties("Parent Entity EntryID") Is Nothing) Then Set userProp = newHistoryTaskItem.UserProperties.Add("Parent Entity EntryID", olText, False, False) userProp.Value = newAcct.EntryID End If

If (newHistoryTaskItem.UserProperties("LinkToOriginal") Is Nothing) Then Set userProp = newHistoryTaskItem.UserProperties.Add("LinkToOriginal", olText, False, False) userProp.Value = newTaskItem.EntryID End If

newHistoryTaskItem.Save

Set newTaskItem = Nothing Set newHistoryTaskItem = Nothing Set newAcct = Nothing Set bcmAccountsFldr = Nothing Set bcmHistoryFolder = Nothing Set bcmRootFolder = Nothing Set olFolders = Nothing Set objNS = Nothing Set olApp = Nothing

End Sub

See Also

About Communication History Items | Office Developer Center: Outlook 2007