Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Creating appointments in Outlook calendar: problem when two accounts exist.


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Feb 28, 2018 2:48 AM Last Post By: roberto nicchi
roberto nicchi

Posts: 95
Registered: 4/11/01
Creating appointments in Outlook calendar: problem when two accounts exist.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 4:01 AM
Hello,

i'm using the code below (found in the net) to create events in Outlook calendar (Outlook 2016)
My customer is saying that in a PC where two accounts exists, some events are created into an account and some are created in the other accounts. Seems a random thing. I guess that the problem is in the GetCalendarFolder method ...
Is there a problem with the code ?

thanks

Roberto

const
  olAppointmentItem = $00000001;
 
  olImportanceLow = 0;
  olImportanceNormal = 1;
  olImportanceHigh = 2;
 
{to find a default Contacts folder}
function GetCalendarFolder(folder: OLEVariant): OLEVariant;
var
  i: Integer;
begin
  for i := 1 to folder.Count do
  begin
    if (folder.Item[i].DefaultItemType = olAppointmentItem) then
      Result := folder.Item[i]
    else
      Result := GetCalendarFolder(folder.Item[i].Folders);
      if not VarIsNull(Result) and not VarIsEmpty(Result) then
        break
  end;
end;
 
function CreateNewAppointment(Subject,Location,body:string;start_date,end_date:TDate;start_time,end_time:TTime;allday:boolean;var EntryId:OLEVariant):shortint;
var
  outlook, ns, folder, appointment:OLEVariant;
  idx: Integer;
begin
  // result=0 evento creato
  // result=1 outlook non trovato
  // result=2 errore mapi
  // result=3 calendario non trovato
  {initialize an Outlook}
 
  try
    outlook := CreateOLEObject('Outlook.Application');
  except
    result:=1;
    exit;
  end;
  try
    {get MAPI namespace}
    try
      ns := outlook.GetNamespace('MAPI');
    except
      result:=2;
      exit;
    end;
 
    try
      {get a default Contacts folder}
      folder := GetCalendarFolder(ns.Folders);
 
      try
        {if Contacts folder is found}
        if not VarIsNull(folder) and not VarIsEmpty(folder) then
        begin
          {create a new item}
          appointment := folder.Items.Add(olAppointmentItem);
 
...........
 
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Creating appointments in Outlook calendar: problem when two accounts exist.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 4:38 AM   in response to: roberto nicchi in response to: roberto nicchi
guess that the problem is in the GetCalendarFolder method ...
I have the same with selecting IN-boxes from outlook because there can be many of them created.
I show the user all the IN-boxes available in a TListbox by looping through the outlook tree and then let the user
select the IN-box. Maybe you can do the same with Calendar folders.
roberto nicchi

Posts: 95
Registered: 4/11/01
Re: Creating appointments in Outlook calendar: problem when two accounts exist.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 8:21 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:
guess that the problem is in the GetCalendarFolder method ...
I have the same with selecting IN-boxes from outlook because there can be many of them created.
I show the user all the IN-boxes available in a TListbox by looping through the outlook tree and then let the user
select the IN-box. Maybe you can do the same with Calendar folders.

Hello

the event is created in the background, it's not an interactive procedure.
I could ask to select the folder to use in a configuration panel.
I'm not sure how to identify each item of the folders tree. Is there a name or some identifier and maybe also some method that do a find with this identifier and returns the corresponding index ?
This solution could be also better than using always the default account calendar, because it's possible to use any calendar configured.

thanks a lot
Roberto
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Creating appointments in Outlook calendar: problem when two accounts exist.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 8:28 AM   in response to: roberto nicchi in response to: roberto nicchi
I could ask to select the folder to use in a configuration panel.
I'm not sure how to identify each item of the folders tree. Is there a name or some identifier and maybe also some method that do a find with this identifier and returns the corresponding index ?

You already have the loop to go through all the folders and get the proper calendar folders (if (folder.Item[i].DefaultItemType = olAppointmentItem) then)
You can get the name by : Folders.Item(i).Name;
Put them in a listbox and remember the i to get the one that the user selects.
roberto nicchi

Posts: 95
Registered: 4/11/01
Re: Creating appointments in Outlook calendar: problem when two accounts exist.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 2:48 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:
I could ask to select the folder to use in a configuration panel.
I'm not sure how to identify each item of the folders tree. Is there a name or some identifier and maybe also some method that do a find with this identifier and returns the corresponding index ?

You already have the loop to go through all the folders and get the proper calendar folders (if (folder.Item[i].DefaultItemType = olAppointmentItem) then)
You can get the name by : Folders.Item(i).Name;
Put them in a listbox and remember the i to get the one that the user selects.

Thanks a lot, i'll try asap.

Roberto

Edited by: roberto nicchi on Feb 28, 2018 2:48 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02