Share via


Select a Project Task

To select a Project Task object, use the Collection.Find method to select an item from the MAPI Folder. The Find method accepts as input a String query and filters items based on the query. If there are more than one item matching the query, only the first match is returned.

The following C# and Visual Basic for Applications (VBA) examples show how to select a Project Task object.

C#

  private void SelectProjectTask()
        {

            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 projects = (Outlook.Folder)bcmRootFolder.Folders["Business Projects"];

            Outlook.Folder projectTasksFolder = (Outlook.Folder)projects.Folders["Project Tasks"];

            string strQuery = "[Subject] = 'Task 1 for Sales Project with Wide World Importers'";

            Outlook.TaskItem projectTaskItem = (Outlook.TaskItem)projectTasksFolder.Items.Find(strQuery);
            if (projectTaskItem != null)
            {
                Console.WriteLine("Project Task found");
                Console.ReadLine();
            }

            else
            {
                Console.WriteLine("Project Task not found");
            }

        }


VBA

  Sub SelectProjectTask()

   Dim olApp As Outlook.Application
   Dim objNS As Outlook.NameSpace
   Dim olFolders As Outlook.Folders
   Dim bcmRootFolder As Outlook.Folder
   Dim bcmProjFolder As Outlook.Folder
   Dim bcmProjectTasksFolder As Outlook.Folder
   Dim existProjectTask As Outlook.TaskItem
   Dim userProp As Outlook.UserProperty

   Set olApp = CreateObject("Outlook.Application")
   Set objNS = olApp.GetNamespace("MAPI")
   Set olFolders = objNS.Session.Folders
   Set bcmRootFolder = olFolders("Business Contact Manager")
   Set bcmProjFolder = bcmRootFolder.Folders("Business Projects")
   Set bcmProjectTasksFolder = bcmProjFolder.Folders("Project Tasks")

   Set existProjectTask = bcmProjectTasksFolder.Items.Find("[Subject] = 'Task 1 for Sales Project with Wide World Importers'")
   If Not TypeName(existProjectTask) = "Nothing" Then

      MsgBox ("Project Task found")

   Else

      MsgBox ("Project Task not found")

   End If

   Set existProjectTask = Nothing
   Set bcmProjFolder = Nothing
   Set bcmProjectTasksFolder = Nothing
   Set bcmRootFolder = Nothing
   Set olFolders = Nothing
   Set objNS = Nothing
   Set olApp = Nothing

End Sub 

See Also

Create a Project Task | Edit a Project Task | Delete a Project Task | 2007 Office System: Updated Developer Content