如何从Outlook日历中获取数据?

时间:2018-12-12 07:01:48

标签: c# outlook office-interop

如何从Microsoft Outlook日历获取数据事件 并将其放在SQL数据库中。 我已经尝试过此代码:

public class SamplesCalendar
{
    public static void Main()
    {
        Outlook.Application msOutlook = new Outlook.Application();
        Outlook.NameSpace session = msOutlook.Session;
        Outlook.Stores stores = session.Stores;
        out
        foreach (Outlook.Store store in stores)
        {
            Outlook.MAPIFolder folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

            folder.GetCalendarExporter();

            Console.WriteLine(folder.FolderPath);

            folder.GetCalendarExporter();
            folder.Items.GetNext();
            LinkedList c = new LinkedList();
            Items CalendarItems = folder.Items;
            for (int i = 0; i <= folder.Items.Count; i++){
                c.AddFirst(1);
            }
            c.printAllNodes();
            Console.WriteLine("Hello");
        }

        Console.ReadLine();
    }
    public class Node
    {
        public Node next;
        public Object data;
    }

    public class LinkedList
    {
        private Node head;

        public void printAllNodes()
        {
            Node current = head;
            while (current != null)
            {
                Console.WriteLine(current.data);
                current = current.next;
            }
        }

        public void AddFirst(Object data)
        {
            Node toAdd = new Node();

            toAdd.data = data;
            toAdd.next = head;

            head = toAdd;
        }

        public void GetAllCalendarItems()
        {
            Microsoft.Office.Interop.Outlook.Application oApp = null;
            Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null;
            Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null;

            oApp = new Microsoft.Office.Interop.Outlook.Application();
            mapiNamespace = oApp.GetNamespace("MAPI"); ;
            CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
            outlookCalendarItems = CalendarFolder.Items;
            outlookCalendarItems.IncludeRecurrences = true;

            foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
            {
                if (item.IsRecurring)
                {
                    Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
                    DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0);
                    DateTime last = new DateTime(2008, 10, 1);
                    Microsoft.Office.Interop.Outlook.AppointmentItem recur = null;

                    for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
                    {
                        try
                        {
                            recur = rp.GetOccurrence(cur);
                            Console.WriteLine(recur.Subject + " -> " + cur.ToLongDateString());
                        }
                        catch
                        { }
                    }
                }
                else
                {
                    Console.WriteLine(item.Subject + " -> " + item.Start.ToLongDateString());
                }
            }

        }
    }
}

我无法从日历中获取任何数据。如何调用获取数据的方法?

2 个答案:

答案 0 :(得分:0)

使用此代码,您可以在Outlook日历中获取所有项目:

        var outlookApplication = new Application();
        NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
        MAPIFolder calendar = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);

        if (calendar != null)
        {
            for (int i = 1; i < calendar.Items.Count + 1; i++)
            {
                var calendarItem = (AppointmentItem)calendar.Items[i];
            }
        }

答案 1 :(得分:0)

您可以使用以下代码读取日历数据:

using System;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace RetrieveAppointment
{
   public class Class1
   {
      public static int Main(string[] args)
      {
         try 
         {
            // Create the Outlook application.
            Outlook.Application oApp = new Outlook.Application();

            // Get the NameSpace and Logon information.
            // Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");
            Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

            //Log on by using a dialog box to choose the profile.
            oNS.Logon(Missing.Value, Missing.Value, true, true); 

            //Alternate logon method that uses a specific profile.
            // TODO: If you use this logon method, 
            // change the profile name to an appropriate value.
            //oNS.Logon("YourValidProfile", Missing.Value, false, true); 

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

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

            // Get the first item.
            Outlook.AppointmentItem oAppt = (Outlook.AppointmentItem) oItems.GetFirst();


            // Show some common properties.
            Console.WriteLine("Subject: " + oAppt.Subject);
            Console.WriteLine("Organizer: " + oAppt.Organizer);
            Console.WriteLine("Start: " + oAppt.Start.ToString());
            Console.WriteLine("End: " + oAppt.End.ToString());
            Console.WriteLine("Location: " + oAppt.Location);
            Console.WriteLine("Recurring: " + oAppt.IsRecurring);

            //Show the item to pause.
            oAppt.Display(true);

            // Done. Log off.
            oNS.Logoff();

            // Clean up.
            oAppt = null;
            oItems = null;
            oCalendar = null;
            oNS = null;
            oApp = null;
         }

            //Simple error handling.
         catch (Exception e)
         {
            Console.WriteLine("{0} Exception caught.", e);
         }  

         //Default return value
         return 0;

      }
   }
}

有关更多信息,请参考此链接:

Read Outlook calender details...