Share via


Adding PIM Items to the Outlook Mobile Database

Send Feedback

Adding a PIM item to the Outlook Mobile database involves calling the item's Save method.

The Outlook Mobile database consists of three separate PIM item lists contained in the three default folders: the Appointments folder, the Tasks folder, and the Contacts folder.

To add a Contact to the Contacts folder

  1. Create an instance of the Outlook Mobile application object and then use it to establish a POOM session. For more information, see How to: Establish a POOM session.

  2. Create a PIM item. For more information, see How to: Create a PIM Item.

  3. Declare a reference to a generic PIM item collection, as follows:

    IPOutlookItemCollection *pItems;
    
  4. Declare a reference to a generic PIM item folder:

    IFolder *pFolder;
    
  5. Use the generic PIM item folder to get the Contacts folder:

    polApp->GetDefaultFolder(olFolderContacts, &pFolder);
    
  6. Use the Contacts folder to get the collection of Contact items:

    pFolder->get_Items(&pItems)
    
  7. Create a new Contact item:

    pItems->Add(&pContact)
    
  8. Initialize the new Contact item's data members:

    pContact->put_FirstName(TEXT("Michael"));
    pContact->put_LastName(TEXT("Angelo"));
    pContact->put_Company(TEXT("Microsoft"));
    pContact->put_FileAs(TEXT("Angelo"));
    
  9. Save the new Contact item to the database:

    pContact->Save();
    

Code Example

The following code example demonstrates how to add a new Contact object to the Contacts folder.

Note   To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

void AddNewContact(IPOutlookApp *polApp)
{
    IContact *pContact;
    IPOutlookItemCollection *pItems;
    IFolder *pFolder;

    hr = polApp->GetDefaultFolder(olFolderContacts, &pFolder);
    if (hr != S_OK) {
        // GetDefaultFolder failed.
        MessageBox(NULL, 
                   _T("GetDefaultFolder failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pFolder->get_Items(&pItems)
    if (hr != S_OK) {
        // get_Items failed.
        MessageBox(NULL, 
                   _T("get_Items failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pItems->Add(&pContact)
    if (hr != S_OK) {
        // Add failed.
        MessageBox(NULL, 
                   _T("Add failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pContact->put_FirstName(TEXT("Michael"));
    if (hr != S_OK) {
        // put_FirstName failed.
        MessageBox(NULL, 
                   _T("put_FirstName failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pContact->put_LastName(TEXT("Angelo"));
    if (hr != S_OK) {
        // put_LastName failed.
        MessageBox(NULL, 
                   _T("put_LastName failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pContact->put_Company(TEXT("Microsoft"));
    if (hr != S_OK) {
        // put_Company failed.
        MessageBox(NULL, 
                   _T("put_Company failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pContact->put_FileAs(TEXT("Angelo"));
    if (hr != S_OK) {
        // put_FileAs failed.
        MessageBox(NULL, 
                   _T("put_FileAs failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    hr = pContact->Save();
    if (hr != S_OK) {
        // Save failed.
        MessageBox(NULL, 
                   _T("Save failed."),
                   _T("Warning"), 
                   MB_OK);
        exit(0);  // Replace with specific error handling.;
    }

    pContact->Release();
    pFolder->Release();
    pItems->Release();
}

See Also

How to Create a PIM Application with POOM | Pocket Outlook Object Model Application Development for Windows Mobile-based Devices | OlDefaultFolders

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.