Results 1 to 3 of 3

Thread: Delete Calendar Items in Outlook VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    7

    Delete Calendar Items in Outlook VBA

    Hello. I am having trouble with a VBA macro I wrote that schedules outlook appointment items. I have it set up to avoid scheduling duplicate appointments- basically each of these automated appointment items has the same text in the appointment body, so the program looks through the default outlook folder for appointment items with that text in the body and it deletes them before it creates new ones. Ideally this should work- old appointments are deleted and replaced with new ones. Simple.

    The problem is that this appears to work on MY computer, but for other users SOME of the appointment items are not deleted. It's like the program is picking and choosing which appointment items to delete. Since it does not delete them all, duplicate appointments are scheduled.

    Below is my code:

    Code:
    Sub SetSchedule()
    
    'Sets up Outlook directories'
    
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    Set mynamespace = olApp.GetNamespace("MAPI")
    Set myfolder = mynamespace.GetDefaultFolder(olFolderCalendar)
    Set myitems = myfolder.Items
    mynamespace.Logon
    Dim olAppt1 As Outlook.AppointmentItem
    
    'set up more variables we will use'
      
    Dim strbody As String
    strbody = "***Automated Appointment***"
    
    'This block of code deletes the appointmentItems'
    
    For Each AppointmentItem In myitems
    If AppointmentItem.Body = strbody Then
    AppointmentItem.Delete
    End If
    Next
    
    'from here the program goes on to set the appointment items'
    'it schedules the appointments without a problem, so i'm leaving the rest out'
    
    msgbox ("Press OK to run the program")
    At this point, while the msgbox is waiting for the click, I can see the outlook calendar window and the appointments that are deleted. On my computer, all of the appointments with "***Automated Appointment***" in the body are deleted. On other people's computers, only some are deleted.

    What am I doing wrong here? Am I not referencing the outlook default folder correctly?

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    7

    Re: Delete Calendar Items in Outlook VBA

    Comon...no takers?

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Delete Calendar Items in Outlook VBA

    Comon...no takers?
    so who do you expect to be up at that time of day?

    Am I not referencing the outlook default folder correctly?
    i doubt it, as the new appointments would be in the wrong folder

    in case some computers are adding extra characters (hidden) or spaces, try using like or instr to compare body string, or match case
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width