A Simple Example of Automating Outlook

By: mykle hoban

Abstract: This example uses the Microsoft Office servers installed with BCB to display the folders listed in outlook and display them.

Question

How do I use the Microsoft Office servers in C++Builder 5 to automate Outlook?

Answer

Here is a simple example of using TOutlookApplication to get a list of folders into a TreeView control and display them when the user double-clicks the TreeView.

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Outlook_2K_SRVR.h"
#include <OleServer.hpp>
#include <ComCtrls.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TOutlookApplication *ola;
  TButton *Button1;
  TTreeView *tv;
  TLabel *Label1;
  TPopupMenu *PopupMenu1;
  TMenuItem *add1;
  TLabel *Label2;
  void __fastcall Button1Click(TObject *Sender);
  void __fastcall tvDblClick(TObject *Sender);
  void __fastcall add1Click(TObject *Sender);
private:	// User declarations
  _NameSpacePtr ns;
public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
  void __fastcall EnumFolders(MAPIFolderPtr fldr, TTreeNode *parent);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Outlook_2K_SRVR"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  ns = OutlookApplication1->GetNamespace(WideString("MAPI"));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MAPIFolderPtr tmp;
  TTreeNode *tmp_node;
  tv->Items->Clear();
  for (int i=1; i<=ns->Folders->Count; i++)
  {
    tmp = ns->Folders->Item(i);
    tmp_node = tv->Items->Add(tv->TopItem,String(tmp->Name));
    tmp_node->Data = tmp->EntryID;
    if (tmp->Folders->Count)
      EnumFolders(tmp,tmp_node);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EnumFolders(MAPIFolderPtr fldr, TTreeNode *parent)
{
  MAPIFolderPtr tmp;
  TTreeNode *tmp_node;
  String str;
  for (int i=1; i<=fldr->Folders->Count; i++)
  {
    tmp = fldr->Folders->Item(i);
    str = tmp->Name;
    tmp_node = tv->Items->AddChild(parent,str);
    tmp_node->Data = tmp->EntryID;
    if (tmp->Folders->Count)
      EnumFolders(tmp,tmp_node);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::tvDblClick(TObject *Sender)
{
  MAPIFolderPtr fldr;
  fldr = ns->GetFolderFromID((wchar_t *)tv->Selected->Data);
  fldr->Display();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::add1Click(TObject *Sender)
{
  if (!tv->Selected) return;
  String fldr_name;
  MAPIFolderPtr fldr = ns->GetFolderFromID((wchar_t *)tv->Selected->Data);
  fldr_name = InputBox("enter.folder.name","folder name","");
  if (fldr_name != "")
    fldr->Folders->Add(WideString(fldr_name));
}
//---------------------------------------------------------------------------


Server Response from: ETNASC01