Microsoft KB Archive/304894

= How to open a message using Outlook object model with Entry ID obtained from MAPI in Visual C++ 6.0 =

Article ID: 304894

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Messaging Application Programming Interface
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Outlook 2000 Service Pack 1
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Visual Studio 6.0 Enterprise Edition

-



This article was previously published under Q304894



INTRODUCTION
This article demonstrates how to use the PR_ENTRYID property obtained from extended MAPI to open and display an item using the Outlook object model.



MORE INFORMATION
To create and build this sample project in Microsoft Visual C++, follow these steps:  Open Visual Studio, create a new Win32 Application named &quot;OOMEntryID&quot;, and then select A Simple Win32 Application.  Open the OOMEntryID.cpp file and replace its entire contents with the code below. Make sure that you change the path to Mso.dll and Msoutl.olb to point to their respective locations for your computer.
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;edk.h&quot;

// Import Outlook object model. // Define this according to the Outlook object model // version you are compiling under.
 * 1) define IMPPROPS rename_namespace(&quot;OL&quot;)

//#define OL2000 // Outlook 2000
 * 1) define OL2002 // Outlook 2002 (2002)

// TODO: Verify that the path to the file is correct. #import &quot;c:\Program Files\Microsoft Office\Office\mso9.dll&quot; IMPPROPS #import &quot;c:\Program Files\Microsoft Office\Office\msoutl9.olb&quot; IMPPROPS // TODO: Verify that the path to the file is correct. #import &quot;c:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll&quot; IMPPROPS #import &quot;c:\Program Files\Microsoft Office\Office10\\msoutl.olb&quot; IMPPROPS
 * 1) if defined(OL2000)
 * 1) elif defined(OL2002)
 * 1) endif

struct StartOle {  StartOle { CoInitialize(NULL); } ~StartOle { CoUninitialize; } } _inst_StartOle;

void dump_com_error(_com_error &e) {   _tprintf(_T(&quot;Oops - hit an error!\n&quot;)); _tprintf(_T(&quot;\a\tCode = %08lx\n&quot;), e.Error); _tprintf(_T(&quot;\a\tCode meaning = %s\n&quot;), e.ErrorMessage); _bstr_t bstrSource(e.Source); _bstr_t bstrDescription(e.Description); _tprintf(_T(&quot;\a\tSource = %s\n&quot;), (LPCTSTR) bstrSource); _tprintf(_T(&quot;\a\tDescription = %s\n&quot;), (LPCTSTR) bstrDescription); }

using namespace OL;

int APIENTRY WinMain(HINSTANCE hInstance,                    HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow) { // MAPI session pointer. LPMAPISESSION pSession = NULL; HRESULT hRes = S_OK; // Please add error checking yourself.

// Initialize MAPI. hRes = MAPIInitialize(NULL);

// Log on to MAPI and get a session pointer. hRes = MAPILogonEx(0, NULL, NULL, MAPI_LOGON_UI, &pSession);

// Open message store. ULONG     cbEIDStore = 0; LPENTRYID lpEIDStore = NULL; IMsgStore *pMsgStore;

hRes = HrMAPIFindDefaultMsgStore(pSession,                  &cbEIDStore,                   &lpEIDStore);

hRes = pSession->OpenMsgStore(NULL,                  cbEIDStore,                   lpEIDStore,                   NULL,                   MAPI_BEST_ACCESS,                   &pMsgStore);

// Open Inbox. ULONG       ulObjType; ULONG       cbEntryID; LPENTRYID   lppEntryID; IMAPIFolder *lpFolder;

hRes = pMsgStore->GetReceiveFolder(NULL,                     NULL,                      &cbEntryID,                      &lppEntryID,                      NULL);

hRes = pMsgStore->OpenEntry(cbEntryID,                lppEntryID,                 NULL,                 MAPI_BEST_ACCESS,                 &ulObjType,                 (LPUNKNOWN *)&lpFolder);

// Get Inbox items. LPMAPITABLE lpContentsTable = NULL; LPSRowSet   pRows = NULL; enum {ePR_SUBJECT, ePR_ENTRYID, NUM_COLS}; static SizedSPropTagArray(NUM_COLS,sptCols) = {NUM_COLS, PR_SUBJECT, PR_ENTRYID};

hRes = lpFolder->GetContentsTable(0,                    &lpContentsTable);

hRes = HrQueryAllRows(lpContentsTable,              (LPSPropTagArray) &sptCols,               NULL,               NULL,               0,               &pRows); if (pRows->cRows > 0) // Make sure there is at least one message. {   // Get the first item in the Inbox and get its entry ID. IMessage   *lpMessage; LPSPropValue pspvEID = NULL;

hRes = pMsgStore->OpenEntry(pRows->aRow[0].lpProps[ePR_ENTRYID].Value.bin.cb,                             (LPENTRYID)pRows->aRow[0].lpProps[ePR_ENTRYID].Value.bin.lpb,                              NULL,                              MAPI_BEST_ACCESS,                              &ulObjType,                              (LPUNKNOWN *)&lpMessage); hRes = HrGetOneProp(lpMessage,              PR_ENTRYID,               &pspvEID); char szEID[1001]; HexFromBin(pspvEID[0].Value.bin.lpb,               pspvEID[0].Value.bin.cb,                szEID);

// Open Outlook item using the entry ID we got from MAPI.

try{ OL::_ApplicationPtr pOutlook(&quot;Outlook.Application&quot;); OL::_NameSpacePtr   pNameSpace = pOutlook->GetNamespace(&quot;MAPI&quot;); OL::_MailItemPtr    pMailItem = NULL;

pMailItem = pNameSpace->GetItemFromID(szEID, vtMissing); if (pMailItem) {       // Display message. pMailItem->Display(vtMissing); }   }

catch (_com_error &e) {     dump_com_error(e); }

UlRelease(lpMessage); } else {   MessageBox(NULL,            &quot;There are no messages in the inbox&quot;,            &quot;Open Item&quot;,            MB_OK); }

pSession->Logoff(NULL, 0L, 0);

FreeProws(pRows); UlRelease(lpContentsTable); UlRelease(lpFolder); UlRelease(pMsgStore); UlRelease(pSession);

return 0; }                      On the Project menu, click Settings. Click the Link tab, and then select Input as the category.

For the Object/Library modules section, add the following. MSVCRT.LIB addrLkup.lib edkguid.lib edkdebug.lib edkmapi.lib edkutils.lib mapi32.lib For the Ignore Libraries section, add libcd.lib and libc.lib.

Close the Project Settings dialog box.  Build and then run the project. The first message in the Inbox will be displayed.

