0
votes

I'm trying to get some calendar appointments from some shared exchange calendars. The first access works fine. But when I call the function again. I get an out of range error.

When I make all global vars to temp var then the I get error "Could not open calendar".

How is it possible to create an Outlook instance and then close it again. So that you can create a new one when you need it?

Thanks

    Outlook.Folder _folder = null;
    Outlook.Application oApp;
    Outlook.Recipient recip;
    private void GetCalendar(string mailAddressOfCalendar)
    {
        if (_folder != null)
        {
            ForceUpdateAddOnCalaendar(_folder);
        }
        else
        {
            oApp = new Outlook.Application();
            Outlook.AddressEntry addrEntry = oApp.Session.CurrentUser.AddressEntry;
            if (addrEntry.Type == "EX")
            {
                recip = oApp.Session.CreateRecipient(mailAddressOfCalendar);
                if (recip.Resolve())
                {
                    try
                    {
                        if (_folder == null)
                        {
                            _folder =
                                oApp.Session.GetSharedDefaultFolder(
                                recip, Outlook.OlDefaultFolders.olFolderCalendar)
                                as Outlook.Folder;
                        }
                        ForceUpdateAddOnCalaendar(_folder);
                    }
                    catch
                    {
                        MessageBox.Show("Could not open calendar",
                            "GetSharedDefaultFolder Example",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                }

            }
        }
    }
1

1 Answers

0
votes

Found the Problem: With this method is works fine. The problem was acatually not the function but the global appoinmentlist i've created. Because I run into a conflict by updating the list while accessing it at the same time. This is why it crashed every second attempt.

    private void GetCalendar(string mailAddressOfCalendar)
    {
        Outlook.Application oApp = new Outlook.Application();
        Outlook.AddressEntry addrEntry = oApp.Session.CurrentUser.AddressEntry;
        if (addrEntry.Type == "EX")
        {
            Outlook.Recipient recip = oApp.Session.CreateRecipient(mailAddressOfCalendar);
            if (recip.Resolve())
            {
                try
                {
                    Outlook.Folder folder =
                        oApp.Session.GetSharedDefaultFolder(
                        recip, Outlook.OlDefaultFolders.olFolderCalendar)
                        as Outlook.Folder;

                    ForceUpdateAddOnCalaendar(folder);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Could not open calendar",
                        e.ToString(),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }
    }