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?
Re: Delete Calendar Items in Outlook VBA
Re: Delete Calendar Items in Outlook VBA
Quote:
Comon...no takers?
so who do you expect to be up at that time of day?
Quote:
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