试图从outlook中获取用户约会并在包含列表框的winform中显示?

时间:2014-11-04 16:04:41

标签: c# listbox outlook-2010

我有一个应用程序从活动目录中获取用户信息:所以我的问题是我在下面的代码中做了正确的事情来处理从outlook获取约会吗?

我使用vs 2008与outlook 2010

 private void ShowUserInformation(SearchResult rs)
    {

        if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
            username.Text = rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["givenName"].Value != null)
            FirstName.Text = rs.GetDirectoryEntry().Properties["givenName"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["initials"].Value != null)
            MiddleName.Text = rs.GetDirectoryEntry().Properties["initials"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["sn"].Value != null)
            LastName.Text = rs.GetDirectoryEntry().Properties["sn"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["mail"].Value != null)
            email.Text = rs.GetDirectoryEntry().Properties["mail"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["company"].Value != null)
            company.Text = rs.GetDirectoryEntry().Properties["company"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["l"].Value != null)
            city.Text = rs.GetDirectoryEntry().Properties["l"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["st"].Value != null)
            state.Text = rs.GetDirectoryEntry().Properties["st"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["co"].Value != null)
            country.Text = rs.GetDirectoryEntry().Properties["co"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["memeberOf = IS Team"].Value != null)
            Groups.Text = rs.GetDirectoryEntry().Properties["memeberOf = IS Team"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["postalCode"].Value != null)
            postalcode.Text = rs.GetDirectoryEntry().Properties["postalCode"].Value.ToString();

        phone.Text = "";
        if (rs.GetDirectoryEntry().Properties["telephoneNumber"].Value != null)
            phone.Text = rs.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString();
        else phone.Text = "";

        if (rs.GetDirectoryEntry().Properties["streetAddress"].Value != null)
            address.Text = rs.GetDirectoryEntry().Properties["streetAddress"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["department"].Value != null)
            department.Text = rs.GetDirectoryEntry().Properties["department"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["description"].Value != null)
            descriptions.Text = rs.GetDirectoryEntry().Properties["description"].Value.ToString();

        mobile.Text = "";
        if (rs.GetDirectoryEntry().Properties["mobile"].Value != null)
            mobile.Text = rs.GetDirectoryEntry().Properties["mobile"].Value.ToString();
        else mobile.Text = "";

        homedirectory.Text = "";
        if (rs.GetDirectoryEntry().Properties["homeDirectory"].Value != null)
            homedirectory.Text = rs.GetDirectoryEntry().Properties["homeDirectory"].Value.ToString();
        else homedirectory.Text = "";

        if (rs.GetDirectoryEntry().Properties["whenCreated"].Value != null)
            WhenCreate.Text = rs.GetDirectoryEntry().Properties["whenCreated"].Value.ToString();

        if (rs.GetDirectoryEntry().Properties["whenChanged"].Value != null)
            whenchanged.Text = rs.GetDirectoryEntry().Properties["whenChanged"].Value.ToString();


    }

现在我正试图从outlook中获取该用户的约会(使用他的邮件属性)并在lisbox中显示,所以我在网上做了一些阅读,并从微软网站上找到了这段代码:

private void DemoAppointmentsInRange()
{
Outlook.Folder calFolder = Application.Session.GetDefaultFolderOutlook.OlDefaultFolders.olFolderCalendar)as Outlook.Folder;
DateTime start = StartTime.Value; //datetimepicker 1 start date (letting the user select the range)
DateTime end = EndTime.Value; //datetimepicker 1 start date (letting the user select the range)
Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
    foreach (Outlook.AppointmentItem appt in rangeAppts)
    {
        Appoinments.Items.Add("Subject: " + appt.Subject 
            + " Start: " + appt.Start.ToString("g"));
    }
}
}

**/// <summary>
/// Get recurring appointments in date range.
/// </summary>
/// <param name="folder"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns>Outlook.Items</returns>**

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; }
    }

谷歌上的常量搜索后,我发现这个代码,我现在使用了这个代码,我可以用自己的任命填写列表框,但现在我的个人约会如何将用户名或用户的电子邮件传递给下面的代码来获取他们的任命?

 private void button2_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Outlook.RecurrencePattern recurrencePattern;
            Microsoft.Office.Interop.Outlook.NameSpace oNS;
            Microsoft.Office.Interop.Outlook.MAPIFolder oCalendar;
            Microsoft.Office.Interop.Outlook.Items oItems;
            Microsoft.Office.Interop.Outlook.AppointmentItem oAppt;
            Microsoft.Office.Interop.Outlook.Application _OutlookApplication;
            try
            {
                _OutlookApplication = new Microsoft.Office.Interop.Outlook.Application();
                oNS = _OutlookApplication.GetNamespace("MAPI");

                // Get the Calendar folder.
                oCalendar = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

                oCalendar.Items.IncludeRecurrences = true;


                // Get the Items (Appointments) collection from the Calendar folder.
                oItems = oCalendar.Items;


                for (Int32 x = 1; x <= oItems.Count; x++)
                {
                    //Need to change how we are getting the appointment
                    //Apparently Outlook will return non-appointments in the calendar feed
                    try
                    {
                        oAppt = (Microsoft.Office.Interop.Outlook.AppointmentItem)oItems[x];
                        Appointments.Items.Add(oAppt.Subject);

                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (oAppt.Body != null)
                        Console.WriteLine("      Calendar Body:" + oAppt.Body.ToString());

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

0 个答案:

没有答案