Link to home
Start Free TrialLog in
Avatar of threesheds
threesheds

asked on

Automating Outlook 98 with MFC

How can I drill down into Outlook 98 using MFC to get a folder item?  I have done it with Word Basic and the Outlook 98 type library, but I need the equivalent using MFC COM (IDispatch).  How can I emulate the VB code:
Set ol = Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set objFolder = olns.GetDefaultFolder(FolderInbox)
etc.
Avatar of PinTail
PinTail
Flag of United Kingdom of Great Britain and Northern Ireland image

Do you know how to use COM and OLE ??
Avatar of threesheds
threesheds

ASKER

PinTail, I have studied "Beginning MFC COM Programming", especially Chap.2, which describes COM interfaces, and accessing Word using IDispatch in detail.  I follow the code OK.
The literature I've read says VB provides a very high level interface for complex tasks, and I want to emulate how it does it, using MFC.

I haven't done this for Outlook, but I did it for Excel a while ago.  You can use type library the following way in MFC:

1. Open class wizard.  2. Click "Add class" button. 3. Select "Import from type library". 4. Browse through your directories to find the type library for Outlook 98.  

One or more C++ classes will be added to your project.  You can use the new class/classes to access Outlook.  The class generated for Excel is not easy to use, maybe Outlook is better.
Liu,

Your answer doesn't give me any new information.

I already put together a dialog-based pgm in MFC, with the following message handler
(I already made .h and .cpp files from msoutl85.olb in Office):

void COutlookDlg::OnDoit()
{
      // Create an Application object
      Application app;
      // Now get its dispatch interface. This will start up the server
      if (!app.CreateDispatch(_T("outlook.application")))
      {            
          AfxMessageBox(_T("Call to CreateDispatch on server failed"));
          return;
      }
}

From here, I don't know how to use the object app, to get the next object, GetNamespace("MAPI"), and then use this object to proceed to Folders, Items, etc., in the way that VB does it.

I am also trying to do it the hard way, using late binding, not using a type library, with CoCreateInstance, QueryInterface, GetIDsOfNames, IDispatchInvoke, etc.

Here is the URL which should be of interest to you. It explains, how to get the MAPI session and other things to extend OutLook application using VC++

http://support.microsoft.com/support/kb/articles/q198/7/25.asp
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
// Start Outlook.
   // If it is already running, you'll use the same instance...
   _Application olApp;
   COleException e;
   if(!olApp.CreateDispatch("Outlook.Application", &e)) {
      CString str;
      str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
      AfxMessageBox(str, MB_SETFOREGROUND);
      return;
   }

   // Logon. Doesn't hurt if you are already running and logged on...
   NameSpace olNs(olApp.GetNamespace("MAPI"));
   
I copied and included the code from:
HOWTO: Automate Outlook Using Visual C++/MFC - http://support.microsoft.com/support/kb/articles/Q220/6/00.asp directly into my dialog button message handler, and it ran, added a Contact, and sent a message to my Outlook 98 Inbox.
The sample has exactly the type of code I was searching for.  This will enable me to emulate the Word VBA program I now have running in a production environment.  I am relieved.

Thank you, naveenkohli.

I put in the following code:

void CWordTestDlg::OnDoit()
{
      // Start Outlook. If it is already running, you'll use the same instance...
   _Application olApp;
   COleException e;
   if(!olApp.CreateDispatch("Outlook.Application", &e)) {
      CString str;
      str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
      AfxMessageBox(str, MB_SETFOREGROUND);
      return;
   }

   // Logon. Doesn't hurt if you are already running and logged on...
   NameSpace olNs(olApp.GetNamespace("MAPI"));
   COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   olNs.Logon(covOptional, covOptional, covOptional, covOptional);
   
     
   // Get the Inbox object:
   MAPIFolder olInbox(olNs.GetDefaultFolder(6)); // Inbox
   
   // Count the Inbox unread items:       
   long MyCount = olInbox.GetUnReadItemCount();      
   CClientDC dc(this);
   CString str;
   int TheCount = (int)MyCount;
   char buf[20];
   str = _itoa(TheCount,buf,10);
   dc.TextOut(10,10,str);

   CString FolderName = olInbox.GetName();
   dc.TextOut(10,30,FolderName);  // Confirms the name "Inbox".

         // Get the Inbox items:       
   MAPIFolder olInboxItems(olInbox.GetItems());

I am stuck at this point, trying to access the first message in Inbox.  Can you help?
I will try to implement this code and see whats happening. It may take some time. I will try to nser ur query tommorrow.

Cheers!
Thank you, naveenkohli.

Meanwhile, I have had small successes like SaveAs, for a MailItem.

I am trying to get a copy of the header file "exchcli.h" from the Exchange Developer's Kit.  I need this to run the sample http://support.microsoft.com/support/kb/articles/q198/7/25.asp which you suggested, which claims to extend Outlook  Rules, which is currently limited to about 40 rules.  I need nearly 200, one for each area code in North America.

I think this sample is the key, because it gets folders from EntryID, and items from the folder, and examines the subject field of the item, etc.  Even if I can't extend the number of Rules, I can go ahead with accessing items..

Regards.

I got the exchcli.h file in the process of downloading another sample.  So the sample http://support.microsoft.com/support/kb/articles/q198/7/25.asp compiles, but it's a dll.
It asks me for an executable.  What should I do?