Link to home
Start Free TrialLog in
Avatar of woodan1
woodan1

asked on

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
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

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
Avatar of woodan1
woodan1

ASKER

I pasted this code into vb in outlook and it states that macros have been disabled.  How can I run this code??
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
Avatar of woodan1

ASKER

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
Avatar of woodan1

ASKER

my objItems.Count =76, but the intX=77 when it errors
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
Avatar of woodan1

ASKER

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.
Avatar of woodan1

ASKER

I'm a little behind your notes, but no that didn't work.
Avatar of Patrick Matthews
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
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of woodan1

ASKER

Bruintje, that did it.
Avatar of woodan1

ASKER

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.
glad it worked :)