Having created an outlook appointment doing something like this;
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook.AppointmentItem appt = null;
app = new Microsoft.Office.Interop.Outlook.Application();
appt = (Microsoft.Office.Interop.Outlook.AppointmentItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
appt.Subject = sub;
appt.Body = body;
appt.Location = Loc;
appt.Recipients.Add(email);
appt.Save();
appt.Send();
Outlook.MailItem mailItem = appt.ForwardAsVcal();
mailItem.To = email;
mailItem.Body = body;
mailItem.Send();
Looking for pointers, as I'm having difficulty finding a definitive answer via Google.
How do I then (at a later date..) retrieve this appointment again from one or more outlook accounts and update some details?
Or is it possible to release a new appointment that will overwrite the existing one?
If you are always accessing the appointment from the same mailbox, store the value of the AppointmentItem.EntryID property and reopen it at a later point using Namespace.GetItemfromID.
If you need to access the appointment from multiple mailboxes, read the value of the AppointmentItem.GlobalAppointmentID. Unfortunately Outlook Object Model would not let you search on a binary property (MAPIFolder.Items.Find). You would need to either drop down to the Extended MAPI level (C++ or Delphi) or use Redemption - see
http://social.msdn.microsoft.com/Forums/en-US/outlookdev/thread/63ea7bda-9767-4145-8ced-78e1095a49f8/
Related
I'm developing an Office VSTO add-in in C# which looks up calendar appointments in Outlook, and reads/writes some data in them.
Recently, one of the clients had issues with the add-in, namely they can't read/write the calendar appointment, and it throws an exception :
The operation failed.
There's not much info from the exception log, but I suspect they have synchronization problems with Exchange.
I asked the client, and they said, that they also have a random popup in Outlook as well, which sometimes happens when there's a mishap while syncing with Exchange. I told them to 'repair' the Outlook data files, but that didn't fix the problem.
The outlook items are basically looked up based on their Outlook EntryIDs OR a Subject (the subjects are unique, and for the sake of simplicity I translated the code a little bit)
...main alghorythm...
Outlook.AppointmentItem calAppointment = null;
calAppointment = SearchforCalendarMatch(EntryID, Subject); //we try to find either by EntryID or by Subject
if (calAppointment != null)
{
calAppointment.Start = StartDate;
calAppointment.End = FinishDate;
calAppointment.Body = Notes;
calAppointment.Save(); //we're changing the found calendar appointment here
}
...
public Outlook.AppointmentItem SearchforCalendarMatch(String EntryID, String Subject)
{
Outlook.NameSpace ns = null;
Outlook.MAPIFolder calendarFolder = null;
Outlook.Items calendarFolderItems = null;
Outlook.Items filteredcalendarFolderItems = null;
Outlook.AppointmentItem calAppointment = null;
Outlook.Application OutlookApp = new Outlook.Application();
outlookversion = OutlookApp.Version;
ns = OutlookApp.Session;
//Try to find the calendar appointment by the EntryID
dynamic OutlookItem = ns.GetItemFromID(t.Text28);
if (OutlookItem != null)
{
if (OutlookItem is Outlook.AppointmentItem)
{
Outlook.AppointmentItem foundItem = (Outlook.AppointmentItem)OutlookItem;
return foundItem;
}
}
//If the EntryID was missing, we try to find the calendar appointment by the Subject.
//(original code is very long, and there are multiple things here, but let's just assume that 100% sure that the subject is unique, so it will find it)
String SubjectMatch = "[Subject] = '" + Subject + "'";
filteredcalendarFolderItems = calendarFolderItems.Restrict(SubjectMatch);
for (int i = 1; i <= filteredcalendarFolderItems.Count; i++)
{
//appointment has to be one of these
calAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)filteredcalendarFolderItems[i];
if (!calAppointment.IsConflict) //conflict check here, not sure if it helps at all
{
return calAppointment; //this is not the complete code, but this is the basic idea of it.
}
}
}
Any ideas how I could make the application recognize these failed Exchange syncs, and handle them differently?
I would still like to sync in these cases, if it's possible... (change the 'local data' in Outlook, then let Outlook handle everything from that on)
You need to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.
I am creating a windows application in C# for setting appointments in a shared calendar for my team. I am the owner of the calendar. I have written the code to set the appointment and set reminder time as 24 hrs before the meeting. But instead of sending reminder to attendee it is sending reminder to me. I am using Microsoft.Office.Interop.Outlook for this. Here is the code that I used:
Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace and Logon information.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Get the Calendar folder.
Outlook.Recipient rcip = oNS.CreateRecipient("abc#domain.com");
Outlook.MAPIFolder oSharedCal = oNS.GetSharedDefaultFolder(rcip, Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.MAPIFolder oShiftCal = oSharedCal.Folders["Sample"];
// Get the Items (Appointments) collection from the Calendar folder.
Outlook.Items oItems = oSharedCal.Items;
Outlook.AppointmentItem oAppt = (Outlook.AppointmentItem)oItems.Add();
// Set Appointment properties.
oAppt.Subject = Subject;
oAppt.Start = start;
oAppt.End = end;
oAppt.RequiredAttendees = email;
oAppt.ReminderMinutesBeforeStart = 24 * 60;
oAppt.ReminderSet = true;
oAppt.BusyStatus = Outlook.OlBusyStatus.olFree;
//Show the item to pause.
oAppt.Save();
oAppt.Send();
Can anyone help me with this?
Reminders cannot be set for attendees, they are for your copy of the appointment in the calendar you create it in. Attendees can set a reminder for themselves when the accept the meeting request.
Let's say I log in to the OS with administrator account and have permissions to set appointments to other users, without sending a mail.
How can I do it in the code?
I could only find examples working with AppontmentItem and set an appointment to the local machine's outlook. How can I do it for external users?
Many thanks in advance!
private static void AddAppointment()
{
Outlook.Application outlookApp = new Outlook.Application(); // creates new outlook app
Outlook.AppointmentItem oAppointment =
(Outlook.AppointmentItem) outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
// creates a new appointment
oAppointment.Subject = "Enquiry Changes made to john enquiry"; // set the subject
oAppointment.Body = "This is where the appointment body of the appointment is written"; // set the body
oAppointment.Location = "Nicks Desk!"; // set the location
oAppointment.Start = DateTime.Now.AddHours(2);
oAppointment.End = DateTime.Now.AddHours(3);
oAppointment.ReminderSet = true; // Set the reminder
oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
oAppointment.Importance = Outlook.OlImportance.olImportanceHigh; // appointment importance
oAppointment.BusyStatus = Outlook.OlBusyStatus.olBusy;
oAppointment.Save();
Outlook.MailItem mailItem = oAppointment.ForwardAsVcal();
}
Use Namespace.GetSharedDefaultFolder() to open other user's Calendar folder, then create an appointment using MAPIFolder.Items.Add.
I am setting the property (for making them As Read and with High Importance) of the mail those are coming to the MS Outlook 2010 inbox using below code -
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
int i = myInbox.Items.Count;
((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).UnRead = false;
((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).Importance = OlImportance.olImportanceHigh;
This works fine when only one mail comes at a time (I can see the mail as Read and with High Importance) after the code execution but when three or four mails coming at a time then it set the property of only one mail not for all the three or four mails.
Please suggest.
Remember to save the message after setting any property.
Most importantly, your code uses multiple dot notation - for each ".", you get back a brand new COM object, so you end up setting Importance property on an object different from the one used to set the UnRead property.
int i = myInbox.Items.Count;
MailItem msg = (Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i];
msg.UnRead = false;
msg.Importance = OlImportance.
msg.Save();
Another problem is that you assume that the last item in the Items collection is the latest one. This is not generally true. As cremor suggested, use Items.ItemAdd event, but still do not forget to save the message.
You can use the ItemAdd event of the Items property of the folder:
Items inboxItems = myInbox.Items;
inboxItems.ItemAdd += HandleItemAdded;
private void HandleItemAdded(object item)
{
MailItem mail = item as MailItem;
if (mail == null) { return; }
mail.UnRead = false;
mail.Importance = OlImportance.olImportanceHigh;
}
I'm trying to implement creating/updating appointments via C# code. I'm using dedicated exchange account and webdav to communicate. Updating appointment require saving this appointment to our tech-user mailbox.
Is there any other option to create and update appointments? Right now I'm struggling limits in quantity of calendar appointment on single mailbox.
I'm sure there should be other option but I can not see it.
Looking to see if anyone else has any suggestions or solutions to this issue.
Thanks
Creating an appointment...
//Give values to Subject,body,....
_service is the Exchange service object.
Appointment app = new Appointment(_service);
app.Subject = subject;
app.Body = body;
app.Start = startTime;
app.End = endTime;
app.Location = location;
DayOfTheWeek[] days = new DayOfTheWeek[] { DayOfTheWeek.Saturday };
app.Recurrence = new Recurrence.WeeklyPattern(app.Start.Date, 1, days);
app.Recurrence.StartDate = app.Start.Date;
app.Recurrence.NumberOfOccurrences = 3;
app.Save();
If you need further information please ask...