Copyright © 2025 IMIBO. Privacy Statement
Request # 26
How to Open another person’s Task folder and post a Task item?
This example requires you to use a Microsoft Exchange account.
The ability to open another person’s folder, and whether or not you can change it, is controlled by the owner of the folder.
Code snip:
function AddTaskItem(const TaskFolder: IMAPIFolder; const Subject: string; const Body: string; const TaskStart, const TaskEnd: TDateTime; const Status: TTaskStatus; const PercentComplete: Double = 0.0; const Reminder: Boolean = False; c onst ReminderTime: TDateTime = 0; const TaskOwner: string = 'Unknown'): Boolean; var hr: HRESULT; TaskItem: IMessage; ConversationIndex: TSBinary; TaskComplete: Boolean; begin Result := False; if not Assigned(TaskFolder) then raise Exception.Create('not Assigned(TaskFolder)'); // Check Subject if (Trim(Subject) = '') then raise Exception.Create('Empty Subject!'); // Check Percent Complete if (PercentComplete < 0.00) or (PercentComplete > 1.00) then raise Exception.Create('Invalid Percent Complete Value!'); ZeroMemory(@ConversationIndex, SizeOf(TSBinary)); try // Create a message. This will set also PR_MESSAGE_CLASS TaskItem := CreateMapiMessage(TaskFolder, oTask); if not Assigned(TaskItem) then exit; // Set Subject SetPropString(TaskItem, PR_SUBJECT, Subject); SetPropString(TaskItem, PR_NORMALIZED_SUBJECT, Subject); SetPropString(TaskItem, PR_CONVERSATION_TOPIC, Subject); // Conversation Index hr := ScCreateConversationIndex(0, nil, ConversationIndex.cb, ConversationIndex.lpb); if failed(hr) then raise EMAPIError.CreateMAPI(nil, hr); SetPropBinary(TaskItem, PR_CONVERSATION_INDEX, ConversationIndex.cb, ConversationIndex.lpb); // Set Body if (Trim(Body) <> '') then SetPropString(TaskItem, PR_BODY, Body); // Set Icon // $500 regular task // $501 recurring task // $502 task assignee's copy // $503 task assigner's copy SetPropLong(TaskItem, PR_ICON_INDEX, $500); // Set Msg Flags SetPropLong(TaskItem, PR_MESSAGE_FLAGS, MSGFLAG_READ); // Set Start Time SetNamedPropSysTime(TaskItem, OutlookCommonNamedProp[idCommonStart].GUID_ID^, OutlookCommonNamedProp[idCommonStart].PROP_ID, TaskStart); SetNamedPropSysTime(TaskItem, OutlookTasksNamedProp[idTaskStartDate].GUID_ID^, OutlookTasksNamedProp[idTaskStartDate].PROP_ID, TaskStart, False); // Set End Time SetNamedPropSysTime(TaskItem, OutlookCommonNamedProp[idCommonEnd].GUID_ID^, OutlookCommonNamedProp[idCommonEnd].PROP_ID, TaskEnd); SetNamedPropSysTime(TaskItem, OutlookTasksNamedProp[idTaskDueDate].GUID_ID^, OutlookTasksNamedProp[idTaskDueDate].PROP_ID, TaskEnd, False); // Set Task Mode (* $00000000 The task is not assigned. $00000001 The task is embedded in a task request. $00000002 The task was accepted by the task assignee. $00000003 The task was rejected by the task assignee. $00000004 The task is embedded in a task update. $00000005 The task was assigned to the task assigner. *) SetNamedPropLong(TaskItem, OutlookCommonNamedProp[idTaskMode].GUID_ID^, OutlookCommonNamedProp[idTaskMode].PROP_ID, 0); // Set Task Status (* $00000000 The user has not started work on the Task object. If the property is set to this value, the value of the idPercentComplete property MUST be 0.0. $00000001 The user's work on this Task object is in progress. If the property is set to this value, the value of the idPercentComplete property MUST be greater than 0.0 and less than 1.0. $00000002 The user's work on this Task object is complete. If the property is set to this value, the value of the idPercentComplete property MUST be 1.0, the value of the idTaskDateCompleted property MUST be the current date, and the value of the idTaskComplete property MUST be $01. $00000003 The user is waiting on somebody else. $00000004 The user has deferred work on the Task object. *) SetNamedPropLong(TaskItem, OutlookTasksNamedProp[idTaskStatus].GUID_ID^, OutlookTasksNamedProp[idTaskStatus].PROP_ID, Ord(Status)); // Set Percent Complete SetNamedPropDouble(TaskItem, OutlookTasksNamedProp[idPercentComplete].GUID_ID^, OutlookTasksNamedProp[idPercentComplete].PROP_ID, PercentComplete); // Set Task Complete TaskComplete := PercentComplete = 1.00; SetNamedPropBoolean(TaskItem, OutlookTasksNamedProp[idTaskComplete].GUID_ID^, OutlookTasksNamedProp[idTaskComplete].PROP_ID, TaskComplete); // Set Reminder SetNamedPropBoolean(TaskItem, OutlookCommonNamedProp[idReminderSet].GUID_ID^, OutlookCommonNamedProp[idReminderSet].PROP_ID, Reminder); // Set Reminder Time and Reminder Delta if Reminder then begin SetNamedPropSysTime(TaskItem, OutlookCommonNamedProp[idReminderTime].GUID_ID^, OutlookCommonNamedProp[idReminderTime].PROP_ID, ReminderTime); SetNamedPropSysTime(TaskItem, OutlookCommonNamedProp[idReminderSignalTime].GUID_ID^, OutlookCommonNamedProp[idReminderSignalTime].PROP_ID, ReminderTime); SetNamedPropLong(TaskItem, OutlookCommonNamedProp[idReminderDelta].GUID_ID^, OutlookCommonNamedProp[idReminderDelta].PROP_ID, 0); end; // Set Task Owner and FixOffline SetNamedPropString(TaskItem, OutlookTasksNamedProp[idTaskOwner].GUID_ID^, OutlookTasksNamedProp[idTaskOwner].PROP_ID, TaskOwner); SetNamedPropBoolean(TaskItem, OutlookTasksNamedProp[idTaskFFixOffline].GUID_ID^, OutlookTasksNamedProp[idTaskFFixOffline].PROP_ID, SameText(TaskOwner, 'Unknown')); // Set Task Ownership SetNamedPropLong(TaskItem, OutlookTasksNamedProp[idTaskOwnership].GUID_ID^, OutlookTasksNamedProp[idTaskOwnership].PROP_ID, Ord(olNewTask)); // Set Task Acceptance State SetNamedPropLong(TaskItem, OutlookTasksNamedProp[idTaskAcceptanceState].GUID_ID^, OutlookTasksNamedProp[idTaskAcceptanceState].PROP_ID, Ord(olTaskNotDelegated)); // Set Task State SetNamedPropLong(TaskItem, OutlookTasksNamedProp[idTaskState].GUID_ID^, OutlookTasksNamedProp[idTaskState].PROP_ID, Ord(oltsNotAssigned)); // Set Side Efect SetNamedPropLong(TaskItem, OutlookCommonNamedProp[idSideEffects].GUID_ID^, OutlookCommonNamedProp[idSideEffects].PROP_ID, seCoerceToInbox or seOpenForCtxMenu); hr := TaskItem.SaveChanges(FORCE_SAVE); if failed(hr) then raise EMAPIError.CreateMAPI(TaskItem, hr); Result := True; finally if Assigned(ConversationIndex.lpb) then MapiFreeBuffer(ConversationIndex.lpb); end; end;
Download Request #26 as Compiled Application
Download Project (DELPHI 10.4) ZIP file
Source Code: In package