Reputation: 12060
The following code sets the categories of appointment items regarding text in subject and body.
oFinalItems
is a selection of calendar entries and contains valid entries.
dicCategories
is a dictionary that holds search strings and the assigned categories:
For Each oAppt In oFinalItems
For Each Key In dicCategories.Keys
If InStr(1, oAppt.Subject & oAppt.Location, Key, vbTextCompare) > 0 Then
strCategory = dicCategories.Item(Key)
If InStr(1, oAppt.Categories, strCategory, vbTextCompare) = 0 Then
If oAppt.Categories = "" Then
oAppt.Categories = strCategory
Else
oAppt.Categories = strCategory & ";" & oAppt.Categories
End If
oAppt.Save
End If
Exit For
End If
Next
Next
I get the error "Object doesn't support this method" in the line
oAppt.Categories = strCategory
This code was running before reinstalling my PC.
I checked the type and it stated "AppointmentItem". I just found out that it only doesn't work for meetings where the invitation came from an external system.
Upvotes: 1
Views: 1187
Reputation: 926
I have finally found the issue: you can not set the category for a recurring appointment, except this is the master/parent appointment. If it isn't the master, you can get to it using the Parent property.
This fixes this issue:
Sub SetCategory(oItem As Object, sCat As String)
If TypeOf oItem Is AppointmentItem Then
If oItem.IsRecurring Then
If oItem.RecurrenceState <> olApptMaster Then
Set oItem = oItem.Parent
End If
End If
End If
oItem.Categories = sCat
oItem.Save
End Sub
Upvotes: 1
Reputation: 49425
If you take a look at the code:
For Each oAppt In oFinalItems
The Items
object may contain different types of items - MailItem, AppointmentItem, MeetingItem, DocumentItem and etc. Not all of them define the Categories
property.
I'd suggest checking the item class or type before accessing any properties. For example:
If TypeName(Item) <> "AppointmentItem" Then
Exit Sub
End If
Upvotes: 1