简体   繁体   中英

how to get data from outlook calendar?

How I can get data events from Microsoft outlook calendar and put it in SQL database. I have tried this code:

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

        }
    }
}

I can't get any data from calendar. How do I call the methods to get the data?

With this code you get all your items in your Outlook-calendar:

        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; i++)
            {
                var calendarItem = (AppointmentItem) calendar.Items[i];
            }
        }

You could read calendar data using the below code:

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;

      }
   }
}

For more information, please refer to this link:

Read Outlook calender details...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM