Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

Access Outlook Public Calendar Folders

I am trying to get calendar items from public folders in Exchange. I have gotten some code to work for the default calendar in Outlook, but could not figure out how to get the public folder items. I searched all over the internet and couldn't find the answer.  Sample code would be helpful, but not required.

I previously asked this question, and "smurff" said:

--------------------------------
The only thing I have is a VB code example of interfacing with the calender public folders in exchange.


You can add appointments to a user's personal calendar folder and to a public folder.

To add an appointment to a folder

Create an Appointment object.
Set the properties of the appointment. A StartTime is required.
Save the appointment to the designated folder by using the Appointment object's IDataSource Interface.
The following example shows how to create an appointment and then save it to a specified folder:

Visual Basic
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Library
Function CreateAppointment(StartTime As Date, _
                           EndTime As Date, _
                           Subject As String, _
                           Location As String, _
                           TextBody As String, _
                           iMbx As IMailbox) As Appointment

   Dim iAppt       As New Appointment
   Dim Conn        As New ADODB.Connection
   Conn.Provider = "ExOLEDB.DataSource"
   
   'Set the appointment properties
   With iAppt
       .StartTime = StartTime
       .EndTime = EndTime
       .Subject = Subject
       .Location = Location
       .TextBody = TextBody
       'Save the appointment
       Conn.Open iMbx.BaseFolder
       .DataSource.SaveToContainer iMbx.Calendar, Conn
   End With
   
   Set CreateAppointment = iAppt
End Function

sorry i dont have a delphi one. Maybe someone could convert for you?
regards
Smurff
--------------------------------

If you can convert that to Delphi, I'll give you and Smurff points.
0
NelsonR
Asked:
NelsonR
  • 3
  • 3
1 Solution
 
SChertkovCommented:
Try following code

var
  Folder, objSession, Messages, Msg: OleVariant;
begin
  objSession := CreateOleObject('MAPI.Session');
  objSession.Logon;
  // List calendar folder
  Folder := objSession.GetDefaultFolder(0); // CdoDefaultFolderCalendar
  Messages := Folder.Messages;
  Msg := Messages.GetFirst;
  while not VarIsEmpty(Msg) do
    begin
      ListBox1.Items.Add(Msg.Subject);
      Msg := Messages.GetNext;
    end;
  // Add new item
  Msg := Folder.Messages.Add;
  Msg.Subject := 'Hello !';
  Msg.StartTime := Now;
  Msg.EndTime := Now + 1;
  Msg.Update;
  objSession.LogOff;
0
 
SChertkovCommented:
See MSDN CDO 1.2.1 (Messaging and collaboration Services)
for details
0
 
NelsonRAuthor Commented:
Note what I said:
-----
I am trying to get calendar items from public folders in Exchange. I have gotten some code to work for
the default calendar in Outlook, but could not figure out how to get the public folder items.
-----

I already have code that works for the default calendar which is what you gave me.  If you know of any way of getting public folders (not objSession.GetDefaultFolder(0)), please let me know.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SChertkovCommented:
Any folder can be open by Session.GetFolder(folderID)
folderID is unique key generated on folder creation.
Following code dump all folders (include public)
and show it FolderID.

var
  objSession: OleVariant;
  Index: Integer;

procedure ShowFolder(Folder: OleVariant; const Pad: String);
var
  Folders: OleVariant;
begin
  ListBox1.Items.Add(Pad + Folder.Name + ' ['+ Folder.ID + ']');
  Folders := Folder.Folders;
  Folder := Folders.GetFirst;
  while not VarIsEmpty(Folder) do
    begin
      ShowFolder(Folder, Pad + ' ');
      Folder := Folders.GetNext;
    end;
end;

begin
  objSession := CreateOleObject('MAPI.Session');
  objSession.Logon;
  // List calendar folder
  for Index := 1 to objSession.InfoStores.Count do
    begin
      ListBox1.Items.Add(objSession.InfoStores[Index].Name);
      ShowFolder(objSession.InfoStores[Index].RootFolder, ' ');
    end;
  objSession.LogOff;
end;
0
 
NelsonRAuthor Commented:
Sorry for the delay... I've been pretty busy lately and haven't had a chance to try it out.  I should be able to do it sometime this week.  I think you've got the right answer.  If it works I'll give you the points.
0
 
NelsonRAuthor Commented:
Been a while.. :)  I had forgotten about this question.  I got it working with

var
 olOutlook, olNameSpace, olFolder, olItems, olRestrict, olItem: OleVariant;
begin
 olOutlook := CreateOleObject('Outlook.Application');
 olNameSpace := olOutlook.GetNameSpace('MAPI');
 olFolder := olNameSpace.Folders('Public Folders').Folders('All Public Folders').Folders('Whatever');
end;

It also works the way you mentioned it (with the ID), but it's easier to read 'Public Folders' than '3209823409823' or something :)

Anyway, I'll give it to you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now