none
VBA Filter on User Property with a Space in Property Name RRS feed

  • Question

  • I am using User Properties to store custom data in Outlook for Appointments/Meetings and Tasks.  I want to programatically retrieve all of the items that contain specific data in the User Property.  I am using a DASL filter.  Everything works fine if the User Property Name has no spaces within the name.  However, I when I created the Property, I created with the name of "Event Reminder".  If I try to use that in the filter, I get an error saying "Cannot parse condition.  Error at "@SQL="...".  If I simply change the space to an underscore character, "Event_Reminder", everything works fine.  Is this a limitation in assigning User Property field names?  Or is there some way I can modify my DASL syntax to allow for spaces.  (I tried putting it in both single and escaped quotes, chr(34), but I couldn't find syntax that would work.  I know I can just change the User Property field name, but just checking to see if there is another way.

    Function GetItemsWithUserProperty(olApp As Object, _
             propertyName As String, _
             propertyValue As Variant, _
             Optional sItemType As String = "Meeting") _
                As Outlook.Items
    
     Dim oFolder As Outlook.Folder
     Dim oItems As Outlook.Items
     Dim oRestItmes As Outlook.Items
     Dim oNameSpace As Namespace
     Dim sFilter, sLine2 As String
     
    ' On Error GoTo clean_up
    On Error GoTo 0  'for debugging purposes
     
     Set oNameSpace = olApp.GetNamespace("MAPI")
     
     'DASL Filter
     ' Note 1: The easiest way to determine the format for the DASL queries is to use the Filter tool in Outlook.
     '         In Outlook, click on the View tab, View Settings, then Filter.
     '         Select the desired items in the 1st 3 tabs and then click on the SQL tab to see the corresponding SQL formats.
     ' Note 2: Chr(34) in the below statement represents the (escaped) enclosing double quotes.
     
     'Filter on User Property
     sFilter = "@SQL=" & Chr(34) & _
               "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/" & propertyName & _
               Chr(34) & " like '%" & propertyValue & "%'"
    
     If sItemType = "Meeting" Or sItemType = "Appointment" Then
        
        If sItemType = "Meeting" Then
            iType = 1
        Else   'sItemType = "Appointment"
            iType = 0
        End If
        
        sFilter = sFilter & " AND " & Chr(34) & "http://schemas.microsoft.com/mapi/id/{00062002-0000-0000-C000-000000000046}/82180003" & Chr(34) & " = " & iType
        
        Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
     
     Else   'if sItemType = "Task"
        Set oFolder = oNameSpace.GetDefaultFolder(olFolderTasks)
     End If
     
     Set GetItemsWithUserProperty = oFolder.Items.Restrict(sFilter)
    
    clean_up:
        Set oFolder = Nothing
        Set oItems = Nothing
        Set oRestItems = Nothing
        Set oNameSpace = Nothing
    
    End Function
    
    

    Wednesday, February 26, 2014 9:08 PM

Answers

  • Hello Prostar 209,

    In a DASL query, if the name of a custom property contains spaces, you must apply Uniform Resource Locator (URL) encoding to each space character and replace the space with "%20". In general, URL encoding applies the same way to characters in a DASL query as in a URL.

    • Marked as answer by Prostar 209 Thursday, February 27, 2014 12:44 AM
    Wednesday, February 26, 2014 9:24 PM

All replies

  • Hello Prostar 209,

    In a DASL query, if the name of a custom property contains spaces, you must apply Uniform Resource Locator (URL) encoding to each space character and replace the space with "%20". In general, URL encoding applies the same way to characters in a DASL query as in a URL.

    • Marked as answer by Prostar 209 Thursday, February 27, 2014 12:44 AM
    Wednesday, February 26, 2014 9:24 PM
  • Eugene, Thank you once again!
    Thursday, February 27, 2014 12:43 AM