none
How to know if I have a meeting now in C#? RRS feed

  • Question

  • Hey,

     How can I know if I have a meeting now that I've organized? I tried the following method but it does not work well:

    private bool DoIHaveAMeetingNow()
    {
    	try
    	{
    		Outlook.Folder calFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
    		computerEmailAddress = this.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress.ToString().ToLower();
    
    		DateTime start = DateTime.Now;
    		DateTime end = DateTime.Now.AddMinutes(1);
    		Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
    		if (rangeAppts != null)
    		{
    			foreach (Outlook.AppointmentItem appt in rangeAppts)
    			{
    				Outlook.AddressEntry organizer = GetMeetingOrganizer(appt);
    				string organizerEmail = organizer.GetExchangeUser().PrimarySmtpAddress;
    
    				if (computerEmailAddress.Equals(organizerEmail.ToLower()))
    				{
    					return true;
    				}
    			}
    		}
    
    		return false;
    	}
    	catch (Exception ex)
    	{
    
    	}
    }
    
    private Outlook.Items GetAppointmentsInRange(Outlook.Folder folder, DateTime startTime, DateTime endTime)
    {
    	string filter = "[Start] >= '"
    		+ startTime.ToString("g")
    		+ "' AND [End] <= '"
    		+ endTime.ToString("g") + "'";
    
    	Debug.WriteLine(filter);
    
    	try
    	{
    		Outlook.Items calItems = folder.Items;
    		calItems.IncludeRecurrences = true;
    		calItems.Sort("[Start]", Type.Missing);
    		Outlook.Items restrictItems = calItems.Restrict(filter);
    		if (restrictItems.Count > 0)
    		{
    			return restrictItems;
    		}
    		else
    		{
    			return null;
    		}
    	}
    	catch { return null; }
    }

    Thanks,

    Seif.


    Seif.

    Sunday, March 2, 2014 10:39 AM

Answers

  • Hello Seif,

    It looks like you have already know how to get all appointments Did you try to debug the code?

    Note, the Items collection may contain various types of Outlook items, not only appointments. I'd suggest adding the code for checking the item's type before the foreach loop. Also I'd recommend replacing the foreach loop with the for.

    Finally, I've noticed that you don't release underlying COM objects instantly in the code. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    • Marked as answer by Seif Ibrahim Monday, March 3, 2014 8:14 AM
    Sunday, March 2, 2014 12:17 PM