How do I delete appointments by category - macro/vb

Anyone have a macro to delete the appointment entries by a preset category with code:
Heres the manual way:
View
Current View
By Category
Select the Category:  SQLData
Delete
woodan1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bruintjeCommented:
Hello woodan1,

you could use something along the lines of this macro by Eric Legault
source: http://groups.google.com/group/microsoft.public.outlook.program_vba/msg/45980946ff0ba774 
--------------
Sub DeleteSpecifiedAppointments()
    Dim objNS As Outlook.NameSpace
    Dim objApptFolder As Outlook.MAPIFolder, objAppt As Outlook.AppointmentItem
    Dim objItems As Outlook.Items, intX As Integer
    Set objNS = Application.GetNamespace("MAPI")
    Set objApptFolder = objNS.GetDefaultFolder(olFolderCalendar)
    Set objItems = objApptFolder.Items

    For intX = 1 To objItems.Count
        Set objAppt = objItems.Item(intX)
        If objAppt.Categories = "SQLData" Then
            objAppt.Delete
        End If
    Next
End Sub
--------------

hope this helps a bit
bruintje
woodan1Author Commented:
I pasted this code into vb in outlook and it states that macros have been disabled.  How can I run this code??
bruintjeCommented:
you can lower yourmacro security settings under tools| macros | security to something else then high
maybe you have to restart outlook for it to take effect but i didn't think so

if you lower it it will be able to run
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

woodan1Author Commented:
I had to restart outlook.

It works but I am getting an error...array index out of bounds

Sub DeleteSpecifiedAppointments(strCat As String)
    Dim objNS As Outlook.NameSpace
    Dim objApptFolder As Outlook.MAPIFolder, objAppt As Outlook.AppointmentItem
    Dim objItems As Outlook.Items, intX As Integer
    Set objNS = Application.GetNamespace("MAPI")
    Set objApptFolder = objNS.GetDefaultFolder(olFolderCalendar)
    Set objItems = objApptFolder.Items

    For intX = 1 To objItems.Count
        Set objAppt = objItems.Item(intX) <<<<<after it's deleted then it errors here.
        If objAppt.Categories = strCat Then
            objAppt.Delete
        End If
    Next
    Set objNS = Nothing
    Set objApptFolder = Nothing
    Set objItems = Nothing
End Sub
woodan1Author Commented:
my objItems.Count =76, but the intX=77 when it errors
bruintjeCommented:
try this line instead of the original from the looks of it has to do with going one item to far in the loop

For intX = 1 To objItems.Count -1
woodan1Author Commented:
If I rerun again after they're all gone, it doesn't error.  It only errors when it is deleting.  Hummm.
To test I reimported the appointments, reran the macro and it errors.  I rerun the delete macro again and it is fine.  Hummmm.
woodan1Author Commented:
I'm a little behind your notes, but no that didn't work.
Patrick MatthewsCommented:
Hi Brian,

>         If objAppt.Categories = "SQLData" Then

That will work just fine, unless an appointment has more than one category associated with it, in which case the appointment's
Categories property is a delimited string of all the associated Categories...

Regards,

Patrick
bruintjeCommented:
have to run it backwards because it will encounter the fact that it is trying to look for the one that is already deleted

Sub DeleteSpecifiedAppointments(strCat As String)
    Dim objNS As Outlook.NameSpace
    Dim objApptFolder As Outlook.MAPIFolder, objAppt As Outlook.AppointmentItem
    Dim objItems As Outlook.Items, intX As Integer
    Set objNS = Application.GetNamespace("MAPI")
    Set objApptFolder = objNS.GetDefaultFolder(olFolderCalendar)
    Set objItems = objApptFolder.Items

    For intX = objItems.Count to 1 step -1
        Set objAppt = objItems.Item(intX)
        If objAppt.Categories = strCat Then
            objAppt.Delete
        End If
    Next
    Set objNS = Nothing
    Set objApptFolder = Nothing
    Set objItems = Nothing
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woodan1Author Commented:
Bruintje, that did it.
woodan1Author Commented:
I am importing data that I am setting as a particular category (it's users proposed vacation days) from a db.  This function will allow me to delete them before repopulating them.  That way they never get out of sync - unless someone messes with the category.  If they do then it's their problem.  The users most likely will not mess with the category - a good bet anyway.

Thanks.
bruintjeCommented:
glad it worked :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.