I want to add a custom property to my calendar items (ideally it will contain a unique ID), so that I can use Restrict to collect instances of recurring appointment items. But while I seem to be able to add the property, I cannot find any way to use the Items.Restrict() method to find items containing the property.
I know that I can get a collection of all the items on the calendar and loop every one to find what I want - but that is the current method that I use and it is way to slow.
I looked at dozens of sites and found conflicting answers about whether this is even possible - but Microsoft seems to think it is (see first link), as well as other people (see second link).
I have used the locals window in debug mode and Restrict is definitely not collecting any objects.
I can only assume that I am doing something wrong in the Column section (based on this "The custom properties must be defined in the folder where you are applying the filter. If the custom properties are only defined in the item, the search will fail" - first link) or the view but I cannot figure out what.
I do know that I cannot use a TableView because it will not include recurrence instances (see third link).
- https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering-a-custom-field
- http://www.outlookcode.com/threads.aspx?forumid=2&messageid=27942
-
Sub AddAndRestrictCustomProperty() Dim NS As Outlook.NameSpace Dim dcal As Folder Dim dCalItmes As Items Dim objd As Items, objc As Items Dim item As Outlook.AppointmentItem Dim upCheck As Outlook.UserProperty Dim udpCheck As Outlook.UserDefinedProperty Set NS = Application.GetNamespace("MAPI") Set dcal = NS.GetDefaultFolder(olFolderCalendar) Set dCalItems = dcal.Items Set item = dCalItems.Add(olAppointmentItem) With item .Subject = "Placeholder Appt" .Start = "2/12/2019 4:30PM" .Body = "nothing" .MeetingStatus = olMeeting .Save End With 'adds custom property Set upCheck = item.UserProperties.Add("userPropCheck", olText, True, olText) upCheck.Value = "testing" Debug.Print item.ItemProperties.item("userPropCheck").Value 'prints "testing" item.Save 'gets instances of custom property in objd dCalItems.Sort "[Start]" dCalItems.IncludeRecurrences = True Set objd = dCalItems.Restrict("[subject] = Placeholder Appt And [Start] >= '2/11/2019' and [Start] <= '2/13/2019'") Debug.Print objd(1).ItemProperties.item("userPropCheck").Value 'prints testing 'setColumns seems to not work for custom properties objd.SetColumns ("userPropCheck, subject, start") 'ERROR: The property "userPropCheck" is unknown error 'Jet Restrict Fails Set objc = dCalItems.Restrict("[userPropCheck] = " & Chr(34) & "testing" & Chr(34)) Debug.Print objc(1).ItemProperties.item("userPropCheck").Value 'ERROR: object variable or with block variable not set error 'Jet Find Fails Set objc = dCalItems.Find("[userPropCheck] = " & Chr(34) & "testing" & Chr(34)) Debug.Print objc(1).ItemProperties.item("userPropCheck").Value 'ERROR: object variable or with block variable not set error 'DSAL Restrict Fails sFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & "= 'testing'" ''"@SQL=" & Chr$(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & " = 'testing'" Set objc = dCalItems.Restrict(sFilter) Debug.Print objc(1).ItemProperties.item("userPropCheck").Value 'ERROR: object variable or with block variable not set error 'DSAL Find Fails sFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & "= 'testing'" ''"@SQL=" & Chr$(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & " = 'testing'" Set objc = dCalItems.Find(sFilter) Debug.Print objc(1).ItemProperties.item("userPropCheck").Value 'ERROR: object variable or with block variable not set error 'THIS WORKS to filter the actual calendar view Set objView = Application.ActiveExplorer.CurrentView objView.Filter = Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & "= 'testing'" objView.Save objView.Apply End Sub
As you can see I am fairly lost. I can add a custom property to the item and then restrict on something other than that property to get the item and then print out the custom property, and I can filter the current view on the custom property using the DSAL view.Filter, but using that in the Restrict also does not work.