0

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).

  1. https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering-a-custom-field
  2. http://www.outlookcode.com/threads.aspx?forumid=2&messageid=27942
  3. https://learn.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-filter-recurring-appointments-and-search-for-a-string-in-the-subject

    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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
casewolf
  • 190
  • 10
  • Try to remove the property type (0000001f): `"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/userPropCheck" & Chr(34) & " = 'testing'` – Dmitry Streblechenko Feb 16 '19 at 20:10
  • Hey Dmitry, I tried that first and got the same error. I tried it again just now and I still getting the same behavior. – casewolf Feb 16 '19 at 22:52
  • 1
    Does it work if you do not include recurrences? – Dmitry Streblechenko Feb 17 '19 at 22:41
  • Interesting. It works perfectly for the example if I set dCalItems.IncludeRecurrences to FALSE. Which, unfortunately, defeats the entire point of the exercise for me (which is why I never thought to try setting it to FALSE in the first place). +1 for finding the error. Looks like I am going to have to ask a new question. – casewolf Feb 19 '19 at 00:25

0 Answers0