Accessing Outlook in Delphi 2005
Saturday, June 25, 2005 09:20 PM
A question was posted to the Delphi newsgroups about accessing Outlook from a .NET application witting in Delphi 2005. This is actually pretty easy, but there are a few catches. I recently worked on a .NET project that used Office objects, and had to struggle a little to get Delphi to work with them, so I'm posting this in the hope it would save people some time.
Microsoft Office exposes a set of COM Automation objects that can be used by macros and external applications. Like other COM objects, these can be accessed by .NET applications. .NET allows you to import a type library and generate an interop assembly. For Office, Microsoft already provides Primary Interop Assemblies (PIAs). A Primary Interop Assembly is a special interop assembly, signed by the published and labeled with the PrimaryInteropAssembly attribute. It is the "official" interop assembly for a specific type library. Office 2003 includes PIAs for its type libraries. The PIAs are installed during a complete installation of office. If you haven't installed them, see this page for installation instructions. You can also download the Office XP PIAs here.
To access Outlook objects, you'll need to reference the Outlook PIA. Unlike other assemblies, PIAs are added by referencing the underlying COM object, rather than by adding a direct reference to the assembly. In Delphi 2005, right-click on the References node in the Project Manager tree, and select the Add Reference menu item. This will open the Add Reference dialog box. Select the COM Imports tab, and locate the Microsoft Outlook object library. For Office 2003, the library's full name is "Microsoft Outlook 11 Object Library". Add the library to the
project's references. This will add a reference to Microsoft.Office.Interop.Outlook.dll, which is the PIA for Outlook.
In a C# project, this would be enough. Delphi 2005 appears to have a problem with locating PIAs, however. If you try to compile the application, you'll get an error because the compiler can't find the assembly. Even though the assembly is in the GAC, Delphi may not be able to find it when compiling the project. You'll need to add some paths to the project's search path. Right click the project name in the Project Manager and select the Options menu item. In the Project Options dialog, click on Directories/Conditionals.
You'll need to add a couple of items to the Search path field: First, add the location of the Outlook interop assembly. You can get the path for the assembly by selecting the reference in the Project Manager and examining the Full Path entry in the Object Inspector. On my computer, the Outlook PIA is located at C:\Windows\Assembly\GAC\microsoft.office.interop.outlook\188.8.131.52__71e9bce111e9429c.
If you try compiling now, Delphi will find the Outlook PIA, but will complain about not finding stdole.dll. You'll need to add a reference to stdole.dll's interop assembly. On my computer, this is located at C:\Windows\Assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a.
Now that all references are set, you can start using Outlook objects in your code. Add "Microsoft.Office.Interop.Outlook" to your uses clause. You can now access Outlook's Application object by creating an instance of the ApplicationClass class. From there, you can access any Outlook object much like you could with COM. For example, the following code lists the name of all appointments in the user's default calendar folder:
App := ApplicationClass.Create;
Cal := App.GetNamespace('MAPI').GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
for i := 1 to Cal.Items.Count do
Appointment := AppointmentItem(Cal.Items[i]);
if Assigned(Appointment) then