Do you have sufficient permissions for accessing that mailbox and its
Calendar folder?
I did a quickie console app using C# and VS2005, Framework 2.0 running on
Outlook 2007.
I hard coded the mailbox recipient and since that Outlook setup has multiple
profiles I had to select an Exchange profile on startup. Other than that it
ran seamlessly and without errors and displayed one at a time all the
appointments.
I created 1 recurring appointment in my sales mailbox, but the principle
should apply even if the folder has many different appointments, mixing
recurring and non-recurring.
The Outlook profile used was my dog's profile against his mailbox on my
Exchange server, so Outlook was logged in as my dog. That profile was able
to access my sales mailbox, but my dog has permissions on that mailbox (he's
my sales manager).
Here's the code I used (appropriate references were set of course):
using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace ConsoleApplication1
{
public partial class Program
{
static void Main(string[] args)
{
bool Foobar = false;
Foobar = GetAppointments();
}
private static bool GetAppointments()
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
Outlook.AppointmentItem oAppt = null;
int intFoo = 0;
//create recipient
Outlook.Recipient oRcp =
oNS.CreateRecipient("(e-mail address removed)");
//Get calendar info (appointments)
Outlook.MAPIFolder oFdr = oNS.GetSharedDefaultFolder(oRcp,
Outlook.OlDefaultFolders.olFolderCalendar);
if (oFdr != null)
{
Outlook.Items oItms = (Outlook.Items)oFdr.Items;
oItms.IncludeRecurrences = true;
string strFilter = "([End] > \'September 1, 2006 12:00 AM\' AND
[Start] <= \'March 1, 2007 12:00 AM\')";
Outlook.Items oApps = oItms.Restrict(strFilter);
oApps.Sort("[Start]", false);
oApps.IncludeRecurrences = true; //blnIncludeRecurrences;
try
{
oAppt = (Outlook.AppointmentItem)oApps.GetFirst();
intFoo = (int)oAppt.Class;
}
catch
{
intFoo = 0;
}
if (intFoo == 26)
{
while (oAppt != null)
{
System.Windows.Forms.MessageBox.Show("Subject = " +
oAppt.Subject + " Start = " +
oAppt.Start.ToString("MMM dd, yyyy HH:mm"));
oAppt = (Outlook.AppointmentItem)oApps.GetNext();
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
Tadwick said:
Hi Ken,
Thanks for the reply. My problem was defining the appointments in VBA as
Outlook.Appointment instead of object. That fixed the issue in VBA.
However, the problem that caused this is that I'm trying to convert my VBA
code to C# and I was getting mixed up with how appointment items are
defined
in the two languages. There is another article
(
http://support.microsoft.com/?kbid=310265) that discusses C# and
recurrences
but I'm still getting the master appt for recurring appointments. This
only
happens to other users' folder, not my own. My code is below. Have you
tried this in dot net 2.0?
Thanks, Tad
public void GetAppointments(String recipient, String entryid)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
//create recipient
Outlook.Recipient oRcp = oNS.CreateRecipient(recipient);
//Get calendar info (appointments)
Outlook.MAPIFolder oFdr = oNS.GetSharedDefaultFolder(oRcp,
Outlook.OlDefaultFolders.olFolderCalendar);
if (oFdr != null)
{
Outlook.Items oItms = (Outlook.Items)oFdr.Items;
strFilter = "([End] > \'September 1, 2006 12:00 AM\' AND [Start] <=
\'March 1, 2007 12:00 AM\')";
Outlook.Items oApps = oItms.Restrict(strFilter);
oApps.Sort("[Start]",false);
oApps.IncludeRecurrences = true;//blnIncludeRecurrences;
Outlook.AppointmentItem oAppt =
(Outlook.AppointmentItem)oApps.GetFirst();
while (oAppt != null)
{
MessageBox.Show("Subject = " + oAppt.Subject + " Start = " +
oAppt.Start.ToString("MMM dd, yyyy HH:mm"));
oAppt = (Outlook.AppointmentItem)oApps.GetNext();
}
}
}