Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    May 2001
    Location
    Patterson Lakes, Melbourne, Victoria, Australia
    Posts
    637
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copy Calendar from Access Db (2002 SP-2)

    I am trying to copy my Access Db Calendar to Outlook. To ensure that deleted appointments are indeed removed, I delete all appointments in Outlook within the given time range. The VBA code snippet is enclosed.
    The problem is that only some of the appointments are deleted. There seems to be o rhyme or reason which ones are deleted. I have stepped through the code execution and the line
    olAppointment.Delete
    is executed for all appointments, yet only some end up actually deleted. Any ideas anyone?

    Code:
    'Set Criteria to range from 1 second before 2 weeks ago to 1 second after 4 months on.
    StDate = DateAdd("d", -14, Date): StDate = DateAdd("s", -1, StDate)
    EndDate = DateAdd("d", 120, Date): EndDate = DateAdd("s", 1, EndDate)
    'Now go through all Outlook appointments to see if lies in the date range. If it
    'is in this date range then delete it, cause it's going to be pasted again anyway.
    For Each olAppointment In olFolder.Items
    If StDate < olAppointment.Start And _
    EndDate > olAppointment.End Then
    'It's within the range of Appnts about to be pasted, so delete it.
    olAppointment.Delete
    End If
    Next olAppointment
    Regards,
    Peter

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Copy Calendar from Access Db (2002 SP-2)

    It is always dangerous to delete items in a collection while using For Each. You are pulling the rug from under the loop as it were. Try looping backwards instead:

    Dim i As Long
    'Set Criteria to range from 1 second before 2 weeks ago to 1 second after 4 months on.
    StDate = DateAdd("d", -14, Date): StDate = DateAdd("s", -1, StDate)
    EndDate = DateAdd("d", 120, Date): EndDate = DateAdd("s", 1, EndDate)
    'Now go through all Outlook appointments to see if lies in the date range. If it
    'is in this date range then delete it, cause it's going to be pasted again anyway.
    For i = olFolder.Items.Count To 1 Step -1
    Set olAppointment = olFolder.Items(i)
    If StDate < olAppointment.Start And _
    EndDate > olAppointment.End Then
    'It's within the range of Appnts about to be pasted, so delete it.
    olAppointment.Delete
    End If
    Next i

    (modified lines are bold)

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Patterson Lakes, Melbourne, Victoria, Australia
    Posts
    637
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Calendar from Access Db (2002 SP-2)

    Hans, once again you have the answer. It works like a charm.
    Thanks mate.
    Regards,
    Peter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •