Extended MAPI in DELPHI
LazyMAPI
Included helper classes
TMAPITable
unit: MAPITable.pas
file path: ..\Library\Helpers\
version: 2026.11
uses Classes, Windows, ActiveX, Types, SysUtils, ExtendedMAPI, IMIEMTypes, MAPIRestriction
TMAPITable is a Delphi wrapper for the IMAPITable interface: the rows of a table (TMAPITableRow) by their index, read in blocks and kept in a cache, a filter (FastFilter, Filter with a TRestriction), sorting (FastSort, Sort), the cursor (GetFirst, GetNext, GetLast, bookmarks) and the table notifications as Delphi events (AllowEvents). It does not cover every method of IMAPITable, but what a list of messages, folders or recipients needs. See also TMAPITableRow, TMAPITableRowField and TMAPITableAdvise (the same unit), and LazyMAPI #6 and LazyMAPI #7 for TMAPITable at work.
Types
TMAPITableRow
TMAPITableRow = class;
TFastFilterRelation
TFastFilterRelation = (rLess, rLessEqual, rGreater, rGreaterEqual, rEqual, rNotEqual, rLike, rPrefix);
The relation of FastFilter: rLess .. rNotEqual compare as RES_PROPERTY; rLike (substring), rPrefix and rEqual on a string compare as RES_CONTENT, case-insensitive.
TTableStatus
TTableStatus = (tblStatUnknown, tblStatComplete, tblStatQChanged, tblStatSorting, tblStatSortError, tblStatSettingCols, tblStatSetcolError, tblStatRestricting, tblStatRestrictError);
The status of the table (IMAPITable.GetStatus): complete, sorting, restricting, setting columns, or an error of one of them.
TSortOrder
TSortOrder = record PropTag: ULONG; OrderDescend: BOOL; end;
One sort key: the column and the direction.
TSortOrders
TSortOrders = array of TSortOrder;
The sort keys of Sort and GetSortOrder, the first one first.
TTableEventType
TTableEventType = (tbleChanged, tbleReload, tbleRestrictDone, tbleSetColDone, tbleSortDone, tbleErrorEvent, tbleRowAdd, tbleRowModified, tbleRowDeleted);
The kind of a table notification.
TTableBasicEvent
TTableBasicEvent = procedure(Sender: TObject;
OnChanged, OnReload, OnRestrictDone, OnSetColDone, OnSortDone.
TTableErrorEvent
TTableErrorEvent = procedure(Sender: TObject;
OnError: the HRESULT of the error.
TTableRowAddOrModifiedEvent
TTableRowAddOrModifiedEvent = procedure(Sender: TObject;
OnRowAdd, OnRowModified: the instance key of the row (RowID) and of the row before it (PriorRowID), the fields of the row; AllowUpdate True lets the table apply the change to its cache.
TTableRowDeletedEvent
TTableRowDeletedEvent = procedure(Sender: TObject;
OnRowDeleted: the instance key of the deleted row; AllowUpdate True lets the table remove it from its cache.
TMAPITableAdvise
TMAPITableAdvise = class(TMAPIBase) private FConnectionID: ULONG_PTR;
TMAPITable
TMAPITable = class(TMAPIBase) protected FIsFiltered: Boolean;
TMAPITableRowField
TMAPITableRowField = class(TMAPIBase) protected FIndex: Integer;
TMAPITableRow
TMAPITableRow = class(TMAPIBase) protected FHash: Integer;
TMAPIVirtualTable
TMAPIVirtualTable = class(TMAPITable) private FTableData: ITableData;
TMAPITable = class(TMAPIBase)
unit MAPITable.pas
| Kind | Declaration | Description |
| constructor | constructor Create(const oTable: IMAPITable); virtual; |
Wraps a MAPI table; rows are read on demand, BatchLoad rows at a time, and cached. Raises EMAPIError for a nil table. |
| destructor | destructor Destroy; override; |
Stops the notifications, frees the cached rows and the restriction, releases the table. |
| property | property Count: Integer Read GetCount; |
The number of rows (GetRowCount; when the provider does not support it, found by seeking); cached until the table changes. |
| property | property Row[const Index: Integer]: TMAPITableRow read GetMAPITableRows; |
The row at position Index (0-based), read with the BatchLoad rows around it and cached; owned by the table. Raises EMAPIError for an index out of range. |
| property | property ColumnsTags: TCardinalDynArray read GetFieldDefs write SetFieldDefs; |
The columns of the table (QueryColumns); setting it calls SetColumns (PR_INSTANCE_KEY is always added) and clears the cache. |
| property | property BatchLoad: word read GetCacheCount write SetCacheCount default 20; |
How many rows are read at a time and cached. |
| property | property WaitForOnBusy: Cardinal read FWaitForOnBusy write FWaitForOnBusy default 2000; |
How long (ms) Sort / Restrict wait for a table that answers MAPI_E_BUSY before they try again. |
| property | property IsFiltered: Boolean read FIsFiltered; |
True while a restriction is applied. |
| property | property TableType: TTableType read GetTableType; |
Snapshot, keyset or dynamic (IMAPITable.GetStatus). |
| property | property TableStatus: TTableStatus read GetTableStatus; |
The status of the table: complete, sorting, restricting, setting columns, or an error of one of them (IMAPITable.GetStatus). |
| property | property AllowEvents: Boolean read FAllowDynamicUpdates write SetAllowDynamicUpdates; |
True: the table is advised for notifications, keeps its cache current and raises the On* events. |
| procedure | procedure FastFilter(const PropTag: ULONG; const Relation: TFastFilterRelation; Value: Variant); |
Restricts the table to the rows where PropTag exists and compares with Value: rLess .. rNotEqual as RES_PROPERTY, rLike / rPrefix / rEqual on a string as RES_CONTENT (substring / prefix / full, case-insensitive). PropTag 0 removes the restriction. Raises EMAPIError when Value cannot be converted or the restriction fails. |
| procedure | procedure FastSort(const PropTag: ULONG; const Ascending: Boolean = True); |
Sorts the table by one column (PR_SUBJECT sorts by PR_NORMALIZED_SUBJECT). Raises EMAPIError on failure. |
| procedure | procedure Refresh(const KeepSortOrder: Boolean = True); virtual; |
Clears the row cache and the count; with KeepSortOrder False the sort order is removed too. |
| function | function GetFirst: TMAPITableRow; |
Moves to the first row and returns it as a new row object the caller frees; nil for an empty table. Raises EMAPIError on failure. |
| function | function GetNext: TMAPITableRow; |
Returns the row at the cursor as a new row object the caller frees and moves on; nil at the end. Raises EMAPIError on failure. |
| function | function GetLast: TMAPITableRow; |
Moves to the last row and returns it as a new row object the caller frees; nil for an empty table. Raises EMAPIError on failure. |
| function | function CreateBookmark: TBOOKMARK; |
A bookmark at the cursor (IMAPITable.CreateBookmark); free it with FreeBookmark. Raises EMAPIError on failure. |
| procedure | procedure FreeBookmark(Position: TBOOKMARK); |
Frees a bookmark of CreateBookmark. Raises EMAPIError on failure. |
| function | function GetBookmarked(Position: TBOOKMARK): TMAPITableRow; |
Moves to a bookmark and returns that row as a new row object the caller frees. Raises EMAPIError on failure. |
| function | function GetSortOrder: TSortOrders; |
The current sort order of the table (QuerySortOrder). Raises EMAPIError on failure. |
| function | function GetSortOder: TSortOrders; |
Deprecated: the old misspelt name of GetSortOrder (no “deprecated” directive: Delphi 6 warns at the method itself). |
| procedure | procedure Sort(const SortOrders: TSortOrders); |
Sorts the table by the given columns (any non-zero ulOrder is taken as descending); waits WaitForOnBusy when the table is busy. Raises EMAPIError on failure. |
| function | function GetFilter: TRestriction; |
The applied restriction as a new TRestriction the caller frees; nil when the table is not filtered. |
| procedure | procedure Filter(const Restriction: TRestriction); |
Applies a restriction (nil removes it); on failure the table is left unfiltered and the error raised. |
| function | function GetRowByID(const RowID: TBytes): TMAPITableRow; |
The row with this instance key (PR_INSTANCE_KEY, FindRow) as a cached row owned by the table; nil when not found. The cursor is restored. |
| function | function GetRowPosition(const RowID: TBytes): Integer; |
The position of the row with this instance key; -1 when not found. |
| function | function FindPositionByEntryID(const EntryID: TBytes): Integer; |
The position of the row whose PR_ENTRYID is EntryID; -1 when not found. |
| function | function GetRowData(const RowID: TBytes; out RowSet: PSRowSet): Boolean; |
The raw row with this instance key (FindRow + QueryRows); free RowSet with FreeProws. The cursor is restored. returns True when exactly one row was found. |
| procedure | procedure Reload(const Table: IMAPITable; const KeepSortOrder: Boolean = True); |
Wraps another MAPI table with the same columns (and sort order when KeepSortOrder) and clears the cache. Raises EMAPIError for a nil table. |
| procedure | procedure AddColumn(const PropTag: ULONG); |
Adds a column to the table (SetColumns); nothing when it is already there. |
| property | property OnChanged: TTableBasicEvent read FOnChanged write FOnChanged; |
TABLE_CHANGED: the table changed in a way that needs a reload. |
| property | property OnError: TTableErrorEvent read FOnError write FOnError; |
TABLE_ERROR: the table reports an error. |
| property | property OnReload: TTableBasicEvent read FOnReload write FOnReload; |
TABLE_RELOAD: the table must be read again. |
| property | property OnRestrictDone: TTableBasicEvent read FOnRestrictDone write FOnRestrictDone; |
TABLE_RESTRICT_DONE: an asynchronous Restrict finished. |
| property | property OnSetColDone: TTableBasicEvent read FOnSetColDone write FOnSetColDone; |
TABLE_SETCOL_DONE: an asynchronous SetColumns finished. |
| property | property OnSortDone: TTableBasicEvent read FOnSortDone write FOnSortDone; |
TABLE_SORT_DONE: an asynchronous SortTable finished. |
| property | property OnRowAdd: TTableRowAddOrModifiedEvent read FOnRowAdd write FOnRowAdd; |
TABLE_ROW_ADDED: a row was added (with the row and its position). |
| property | property OnRowModified: TTableRowAddOrModifiedEvent read FOnRowModified write FOnRowModified; |
TABLE_ROW_MODIFIED: a row changed (with the row and its position). |
| property | property OnRowDeleted: TTableRowDeletedEvent read FOnRowDeleted write FOnRowDeleted; |
TABLE_ROW_DELETED: a row was deleted (with its instance key). |
TMAPIVirtualTable = class(TMAPITable)
unit MAPITable.pas
| Kind | Declaration | Description |
| constructor | constructor Create(const ColumnsTags: TCardinalDynArray; TableType: TTableType = tblTypeDynamic); reintroduce; virtual; |
A table the program fills itself (CreateTable / ITableData) with these columns (PR_INSTANCE_KEY added); its view is this TMAPITable. Raises EMAPIError on failure. |
| destructor | destructor Destroy; override; |
Releases the table data. |
