Click here to Skip to main content
12,998,483 members (112,811 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 4 Oct 2004

Microsoft Outlook Add-in With DialogBox and Database Connectivity

, 4 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Microsoft Outlook add-in with dialogbox and database connectivity.


Please do read my previous tutorials on add-ins, to proceed. ADODB concepts should also be known before proceeding.


Previously, I had published two tutorials on add-ins, and now it is time to move further deep, that is using other techniques. In this tutorial, we are going to have some details added in an Access database, view and add details in that database through add-in. We use ADODB connectivity.

Since this tutorial shows how to perform reading and finding a specific value in the database, the same can be applied for deletion and modification. Maybe it will be added soon. This tutorial also tells you how to invoke Inspector Events.


The ADODB library is a small, lightweight library that contains core objects, and offers the basics for making connections, issuing commands, and retrieving recordsets, and it also enables recordset navigation. You can use it to perform basic maintenance tasks, such as modifying, adding, and deleting records. The nonhierarchical design of this library makes it easy for beginners to use.

Using The Code


The database is implemented in CMyDatabase class.

  1. To use ADODB Connection, first we have to import ADODB into our program, and so the line responsible for this is:
    #import "C:\Program Files\Common Files\System\ADO\msado15.dll " 
    no_namespace name ("EOF","EndOfFile")

    The path may vary from system to system. Search for msado15.dll and put the path there if there's an error.

  2. Two basic things needed in ADODB is the Connection Pointer and the RecordSet Pointer.
    _ConnectionPtr m_pCon;
    _RecordsetPtr m_pRs;
  3. Now we have to establish a connection to our database. The GetDefaultConnect() does this.
    void CMyDatabase::GetDefaultConnect()
        //create an instance for ADODB connection
        HRESULT hr=m_pCon.CreateInstance("ADODB.Connection");
        if( SUCCEEDED(hr) )
            //open the Database
                     Data Source=C:\\Address.mdb;", "","",adModeUnknown);
  4. So now after opening, we can read the values as follows:
    void CMyDatabase::Read(CStringArray* values)
        //connect the database
        _bstr_t bstrQuery;
        _variant_t isAffected;
        _variant_t vEmail;
        _variant_t vName;
        //the query we want to execute
        bstrQuery="SELECT * FROM Addr";
        //Execute the query and it will return 
        //a RecordSet so that we can scroll
        //through the result one by one
        m_pRs=m_pCon->Execute(_bstr_t(bstrQuery), &isAffected, 
        //traverse the recordset till the end of file
        while( !m_pRs->GetEndOfFile() )
            //collect the values by the order 
            //of the field names (i.e)Coulmn names
            //store them in stringarray
            values->Add( (char*) ((_bstr_t)vEmail) );
            values->Add( (char*) ((_bstr_t)vName) );
            //move to next record
        //close the recordset
  5. Adding a new data to the database is as follows:
    void CMyDatabase::Add(PSTR pMail, PSTR pName)
        //Connect to the database
        _variant_t vNull;
        _bstr_t bstrQuery("SELECT * FROM Addr");
        long rIndices[2];
        //create the recordset
        //put our Active Database Connection to our Connection Pointer
        //open the connection successfully with the query
        //first add the field names which is in our database
        COleSafeArray vaFields;
        //next add the values for the above added field names respectively
        COleSafeArray vaValues;
        //now add the field name and it's respective value to the database
        //close the connection
        AfxMessageBox("Details Added");
  6. If you change the query with "WHERE" option, you can perform the search operation.

Inspector Event

There are two windows in Microsoft Outlook:

  1. Explorer window
  2. Inspector window

Both have their separate callbacks and events. Once a mail is clicked in Inbox or Outbox, the window which opens is called as the Inspector window.

Just think how nice it would be to track this New Inspector Event??

The procedure is as follows:

  1. Our add-in class should implement the InspectorEvents to track them, so add this line in the Implements part:
    CAddin :
        public IDispEventSimpleImpl<1,CAddin,&__uuidof(Outlook::InspectorsEvents)>
  2. The job is made simpler by typedef:
    typedef IDispEventSimpleImpl</*nID =*/ 1,CAddin, 
        &__uuidof(Outlook::InspectorsEvents)> MyInspectorsEvents;
  3. Add it in the Sink Entry:
      SINK_ENTRY_INFO(1, __uuidof(Outlook::InspectorsEvents), 
          /*dispid*/ 0xf001, NewInspector, &OnInspectorInfo)

    The OnInspector function is an external function which is going to hold the information about our Inspector Function, and is written as:

    extern _ATL_FUNC_INFO OnInspectorInfo;

    NewInspector is our actual function which will be invoked when a New Inspector event occurs.

  4. From our Application Object, we get the Inspectors object:
  5. Finally, advise the Inspector event:
    HRESULT hr=MyInspectorsEvents::DispEventAdvise((IDispatch*)spInspectors);
  6. The event function info is as follows:

  7. The event function is as follows:
    void __stdcall CAddin::NewInspector(IDispatch* pdispInspector)
      AfxMessageBox("New Inspector Event Occured");

That's it!...we have completed another tutorial with database connectivity!


Microsoft Developer Network (MSDN)

My Contact


This tutorial was checked in Office XP, and it works in Office 2000 if the import files are changed as given in previous tutorials. I can't guarantee whether it works in Office 2003.

If somebody has an add-in written for Office 2003, please do send it to me. I tried a tutorial given in CodeProject but it is not working.!!!!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer
New Zealand New Zealand
Chakkaradeep, known as Chaks to his friends, hails from the Indian subcontinent.Having done his Masters in Software Engineering, he knows his way around a computer and has sound knowledge of Microsoft technologies.

Chaks is currently working as a Microsoft Developer @ New Zealand.

You can reach him via his blog -

You may also be interested in...

Comments and Discussions

Generalproblem while opening 2 mails [modified] Pin
knowledgegate30-Jan-07 23:01
memberknowledgegate30-Jan-07 23:01 
Generalinspector window close event Pin
sarimbaig7-Aug-05 3:47
membersarimbaig7-Aug-05 3:47 
GeneralRe: inspector window close event Pin
Idrassi Mounir22-Sep-06 2:14
memberIdrassi Mounir22-Sep-06 2:14 
GeneralThis addin works for Office 2003 Pin
yuhaian21-Jun-05 12:41
memberyuhaian21-Jun-05 12:41 
Questionhow to catch the folder add event in outlook? Pin
sani chabiy o27-Mar-05 12:56
membersani chabiy o27-Mar-05 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 4 Oct 2004
Article Copyright 2004 by chakkaradeepcc
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid