Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

In Sync with Outlook

This example is a modified version of the standard Qt addressbook example.

It demonstrates the use of QAxObject and querySubObject to instantiate and navigate the Outlook Object Model, and the use of the Qt property system to read and write values of items in the Outlook contact folder.

The modifications in the class declaration of the central widget are a forward declaration of the QAxObject class and the IDispatch interface, and a new QListViewItem subclass ABListViewItem that implements a constructor and a destructor and has a member contact_item of type QAxObject.

    class QAxObject;
    struct IDispatch;

    class ABListViewItem : public QListViewItem
    {
    public:
        ABListViewItem( QListView *listview, QString firstName, QString lastName, QString address, QString eMail, QAxObject *contact );
        ~ABListViewItem();

        QAxObject *contactItem() const;

    private:
        QAxObject *contact_item;
    };

The ABCentralWidget gets a destructor, a new protected function setupOutlook, a new protected slot updateOutlook, and also three members of type QAxObject.

        void findEntries();

        void updateOutlook();

    protected:
        void setupTabWidget();
        void setupListView();
        void setupOutlook();

        QAxObject *outlook, *outlookSession, *contactItems;

        QGridLayout *mainGrid;

The implementation of the ABListViewItem class is trivial:

    ABListViewItem::ABListViewItem( QListView *listview,
                                    QString firstName,
                                    QString lastName,
                                    QString address,
                                    QString eMail,
                                    QAxObject *contact )
    : QListViewItem( listview, firstName, lastName, address, eMail ), contact_item( contact )
    {
    }

    ABListViewItem::~ABListViewItem()
    {
        delete contact_item;
    }

    QAxObject *ABListViewItem::contactItem() const
    {
        return contact_item;
    }
The ABCentralWidget constructor initializes the QAxObject pointers to zero and calls the setupOutlook function. The ABCentralWidget destructor calls the Logoff method of the outlookSession object.
    ABCentralWidget::ABCentralWidget( QWidget *parent, const char *name )
        : QWidget( parent, name ), outlook( 0 ), outlookSession( 0 ), contactItems( 0 )
    {
        mainGrid = new QGridLayout( this, 2, 1, 5, 5 );

        setupTabWidget();
        setupListView();
        setupOutlook();

        mainGrid->setRowStretch( 0, 0 );
        mainGrid->setRowStretch( 1, 1 );
    }

    ABCentralWidget::~ABCentralWidget()
    {
        if ( outlookSession )
            outlookSession->dynamicCall( "Logoff()" );
    }
The setupOutlook implementation creates a QAxObject to wrap the Outlook.Application COM object.
    void ABCentralWidget::setupOutlook()
    {
        outlook = new QAxObject( "Outlook.Application", this );
The call to querySubObject returns a new QAxObject wrapper around the "Session" object of the Outlook Object hierarchy. If the call fails for some reason setupOutlook returns, otherwise it calls the "Logon" method of the Session object.
        // Get a session object
        outlookSession = outlook->querySubObject( "Session" );
        if ( !outlookSession )
            return;
        // Login; doesn't hurt if you are already running and logged on...
        outlookSession->dynamicCall( "Logon()" );
The following call to querySubObject returns a new QAxObject wrapper around the default folder for "contacts".
        // Get the default folder for contacts
        QAxObject *defFolder = outlookSession->querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" );
querySubObject is then used again to get the list of all items in the folder. The connect statement connects the new ABCentralWidget slot to the signals provided by the "Items" COM object. Finally, it calls the updateOutlook function to populate the listview.
        // Get all items
        if ( defFolder ) {
            contactItems = defFolder->querySubObject( "Items" );
            connect( contactItems, SIGNAL(ItemAdd(IDispatch*)), this, SLOT(updateOutlook()) );
            connect( contactItems, SIGNAL(ItemChange(IDispatch*)), this, SLOT(updateOutlook()) );
            connect( contactItems, SIGNAL(ItemRemove()), this, SLOT(updateOutlook()) );
        }

        updateOutlook();
    }

The implementation of the updateOutlook slot clears the listview, and uses querySubObject to iterate through the list of items. For every item provided a new ABListViewItem object is created and filled with the properties of the item object. The object returned by querySubObject is a child of the callee (ie. "contactItems"), but the list view item should take ownership to provide a cleaner relation between entries, so the item has to be removed from its parent object.

    void ABCentralWidget::updateOutlook()
    {
        listView->clear();
        if ( !contactItems )
            return;

        QAxObject *item = contactItems->querySubObject( "GetFirst()" );
        while ( item ) {
            QString firstName = item->property( "FirstName" ).toString();
            QString lastName = item->property( "LastName" ).toString();
            QString address = item->property( "HomeAddress" ).toString();
            QString email = item->property( "Email1Address" ).toString();

            (void)new ABListViewItem( listView, firstName, lastName, address, email, item );
            // the listviewitem takes ownership
            item->parent()->removeChild( item );

            item = contactItems->querySubObject( "GetNext()" );
        }
    }

The addEntry implementation calls the CreateItem method of the Outlook.Application object to create a new contact item, and creates a new ABListViewItem if the call succeeds.

    void ABCentralWidget::addEntry()
    {
        if ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
             !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) {
            QAxObject *contactItem = outlook->querySubObject( "CreateItem(OlItemType)", "olContactItem" );
            if ( contactItem ) {
                contactItem->setProperty( "FirstName", iFirstName->text() );
                contactItem->setProperty( "LastName", iLastName->text() );
                contactItem->setProperty( "HomeAddress", iAddress->text() );
                contactItem->setProperty( "Email1Address", iEMail->text() );
                contactItem->dynamicCall( "Save()" );

                new ABListViewItem( listView, iFirstName->text(),
                    iLastName->text(), iAddress->text(), iEMail->text(), contactItem );
            }
        }

        iFirstName->setText( "" );
        iLastName->setText( "" );
        iAddress->setText( "" );
        iEMail->setText( "" );
    }

The changeEntry implementation updates the values in the contact item of the current listview item as well as the values of the listview item itself.

    void ABCentralWidget::changeEntry()
    {
        ABListViewItem *item = (ABListViewItem*)listView->currentItem();

        if ( item &&
             ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
               !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) ) {

            QAxObject *contactItem = item->contactItem();
            contactItem->setProperty( "FirstName", iFirstName->text() );
            contactItem->setProperty( "LastName", iLastName->text() );
            contactItem->setProperty( "HomeAddress", iAddress->text() );
            contactItem->setProperty( "Email1Address", iEMail->text() );
            contactItem->dynamicCall( "Save()" );

            item->setText( 0, iFirstName->text() );
            item->setText( 1, iLastName->text() );
            item->setText( 2, iAddress->text() );
            item->setText( 3, iEMail->text() );
        }
    }

To build the example you must first build the QAxContainer library. Then run your make tool in examples/qutlook and run the resulting qutlok.exe.

See also The QAxContainer Examples.


Copyright © 2007 TrolltechTrademarks
Qt 3.3.8