How to use the Outlook object model in an Exchange Client extension (260216)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)
  • Microsoft Outlook 2000
  • Microsoft Outlook 2002
  • Microsoft Outlook 97
  • Microsoft Outlook 98
  • Microsoft Office Outlook 2003

This article was previously published under Q260216

SUMMARY

This article demonstrates how to access the Outlook Object Model from within an Exchange Client Extension. This is useful because many operations on e-mail messages and folders can only be performed with the Outlook Object Model.

MORE INFORMATION

To access the Outlook Object Model from within an extension, you can use the IOutlookExtCallback interface which is provided by Microsoft for this purpose. To use this interface, follow these steps:
  1. Create a simple DLL project by following steps 1 through 8 in the following article in the Microsoft Knowledge Base:

    285999 How to build a minimal Exchange client extension by using Visual C++

    Note Before you build the DLL and add the registry values (steps 9 through 13 in Q285999), complete the remainder of the steps in this article.
  2. Add to the project a header file, save the file in the project folder with a name such as "OutlookInterface.h", and paste the following code in the body of the header file:
    #pragma once
    
    #if defined(WIN32) && !defined(MAC)
    
    #ifndef __IOutlookExtCallback_FWD_DEFINED__
    #define __IOutlookExtCallback_FWD_DEFINED__
    typedef interface IOutlookExtCallback IOutlookExtCallback;
    #endif    /* __IOutlookExtCallback_FWD_DEFINED__ */ 
    
    // Outlook defines this interface as an alternate to IExchExtCallback.
    #ifndef __IOutlookExtCallback_INTERFACE_DEFINED__
    #define __IOutlookExtCallback_INTERFACE_DEFINED__
    
    EXTERN_C const IID IID_IOutlookExtCallback;
    
        interface DECLSPEC_UUID("0006720D-0000-0000-C000-000000000046")
        IOutlookExtCallback : public IUnknown
        {
        public:
            virtual HRESULT STDMETHODCALLTYPE GetObject( 
                /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunk) = 0;
            
            virtual HRESULT STDMETHODCALLTYPE GetOfficeCharacter( 
                /* [out] */ void __RPC_FAR *__RPC_FAR *ppmsotfc) = 0;
        };
    
    DEFINE_GUID(IID_IOutlookExtCallback,
        0x0006720d,
        0x0000,
        0x0000,
        0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
    
    #endif    /* __IOutlookExtCallback_INTERFACE_DEFINED__ */ 
    
    #endif // defined(WIN32) && !defined(MAC)
  3. To the main project .cpp file, add a #include statement for the header file you created in step 2; for example:
       #include "OutlookInterface.h"
  4. After the #include statements, add the following #import statements to define the path to the Outlook library files, adjusting the paths as needed:
       //Path to Outlook object model files.
       //For Outlook 2000, use the following lines.
      #import "D:\\Office\\Office\\mso9.dll" no_namespace, named_guids, rename("DocumentProperties", "DocProps" )
      #import "D:\\Office\\Office\\msoutl9.olb" no_namespace exclude("_IRecipientControl", "_DRecipientControl")
       
    
      //For Outlook 2002, comment the lines above and uncomment the next 2 lines.
      // #import "E:\program files\microsoft office\office10\mso.dll" no_namespace, named_guids, rename("DocumentProperties", "DocProps" )
      // #import "E:\\Program Files\\Microsoft Office\\Office10\\msoutl.olb" no_namespace exclude("_IRecipientControl", "_DRecipientControl")<BR/>
  5. After the external declaration for the ExchEntryPoint function, add a prototype statement for the GetOutlookVersion function:
    void GetOutlookVersion(IExchExtCallback *pmecb);
  6. In the Install function, paste the following code before the Return statement to call the GetOutlookVersion function:
       //Call function to display Outlook version.
       GetOutlookVersion(pmecb);
  7. At the end of the .cpp file, insert the following code to define the GetOutlookVersion function:
       void GetOutlookVersion(IExchExtCallback *pmecb)
       {
       HRESULT hRes;
       _ApplicationPtr pOutlookApp;
       LPDISPATCH lpMyDispatch;
       IOutlookExtCallback *pOutlook = NULL;
       hRes = pmecb->QueryInterface(IID_IOutlookExtCallback,(void **) &pOutlook);
    
       //If we are not running Outlook, pOutlook will be null.
       if (pOutlook)
          {
          IUnknown *pUnk = NULL;
          pOutlook->GetObject(&pUnk);
    
          if (pUnk != NULL)
             {
             hRes = pUnk->QueryInterface(IID_IDispatch, (void **) &lpMyDispatch);
             pUnk->Release();
             }
       		
          if (lpMyDispatch)
             {
             pOutlookApp = lpMyDispatch;
             if (pOutlookApp)
          	    {
       	    _bstr_t szOLVersion = "Outlook Version is: " + pOutlookApp->Version;
                MessageBox(NULL, (char*)szOLVersion, "Outlook Version", MB_OK);
       	    }
             }
          }
       }
  8. Follow steps 9 through 13 in Q285999 to finish building the extension DLL, add the required registry keys, and so on.

REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

286408 Description of Outlook and Exchange client extensions

285999 How to build a minimal Exchange client extension by using Visual C++


Modification Type:MinorLast Reviewed:1/13/2006
Keywords:kbemail kbProgramming kbhowto kbMsg kbOutlookObj KB260216