PDA

View Full Version : Search and Count Categories Outlook VBA



specialk9203
06-11-2014, 02:20 PM
Hi,

I am having issues putting together some code that will search through my calendar or a shared calendar for items that are marked green and yellow. I would aslo need to create a counter for each and supply a date parameter. Any ideas?? I am new to using VBA in outlook.

Thanks,
Mike

specialk9203
06-12-2014, 09:06 AM
Here is my code, I am receiving 0 results unfortunately. any help would be appreciated.

Sub CategoryFinder()
Dim Appt As Outlook.AppointmentItem
Dim cntYellow As Integer
Dim cntBlue As Integer
Dim Items As Outlook.Items
Dim Calendar As Outlook.Folder
Set Calendar = Session.GetDefaultFolder(olFolderCalendar)
Set Items = Calendar.Items
cntBlue = 0
For Each Appt In Items
On Error Resume Next
If Appt.End < Now() And Appt.End > Now() - 3 And Appt.Categories = "Assesment" Then
cntBlue = cntBlue + 1
End If
Next Appt
Debug.Print cntBlue
Set Appt = Nothing
End Sub

Charlize
06-12-2014, 10:23 AM
Sub CategoryFinder()
Dim Appt As Outlook.AppointmentItem
Dim cntYellow As Integer
Dim cntBlue As Integer
Dim Items As Outlook.Items
Dim Calendar As Outlook.Folder
'added a datevariable to hold the date of now - 3
Dim mydate As Date
'halt on errors
On Error GoTo 0
Set Calendar = Session.GetDefaultFolder(olFolderCalendar)
Set Items = Calendar.Items
cntBlue = 0
mydate = Now - 3
For Each Appt In Items
' On Error Resume Next
' If Appt.End < Now() And Appt.End > (Now() - 3) And Appt.Categories = "Assesment" Then
If Appt.End < Now() And Appt.End > mydate And Appt.Categories = "Assesment" Then cntBlue = cntBlue + 1
' End If
Next Appt
Debug.Print cntBlue
Set Appt = Nothing
End Sub

specialk9203
06-12-2014, 11:08 AM
Thanks Charlize, that helps. Instead of using "assesments" as my parameter in my if statement, how would you reference a colored category, such as blue or yellow as noted in the variables?

westconn1
06-12-2014, 02:22 PM
for items that are marked green and yellowwhich part is colour marked?

Charlize
06-12-2014, 03:32 PM
@ westconn1

I think he want's to use the color of the categorie instead of the name. And that's a tricky one, I know you can define an object of categorie and there is an olcategorycolor (or something in that way). According to me he would have to loop through the categories and see if it's attached to a calendaritem, then loop through all the names of the categories and if the name is equal then get the color of the name. Only a guess of my part but I think it's not that easy to use the colors of a categorie.

Charlize