VBA Advanced Filter Error

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

VBA Advanced Filter Error

Post by jstevens »

I am receiving the following error message when I try to run code for an advanced filter: Cannot parse condition. Error at "CI_STARTWITH".

Code: Select all

Sub FilterOnInbox()
    Dim olFolder As Object
    Dim filteredItems As Object
    Dim folderItems As Object
    Dim strFilter As String
    Dim itm As Object
    Dim str As String

    Set olFolder = Application.Session.GetDefaultFolder(olFolderInbox)   'Inbox

'Looks within Inbox for messages sent by "My Stuff"
strFilter = "((""http://schemas.microsoft.com/mapi/proptag/0x0065001f"" CI_STARTSWITH 'My' AND ""http://schemas.microsoft.com/mapi/proptag/0x0065001f"" CI_STARTSWITH 'Stuff') OR (""http://schemas.microsoft.com/mapi/proptag/0x0042001f"" CI_STARTSWITH 'My' AND ""http://schemas.microsoft.com/mapi/proptag/0x0042001f"" CI_STARTSWITH 'Stuff'))"

    Set filteredItems = olFolder.Items.Restrict(strFilter)
    
    If filteredItems.count = 0 Then
        str = "Not found"
    Else
        For Each itm In filteredItems
            str = str & itm.Subject & vbCrLf
        Next
    End If
    MsgBox str

Your suggestions are appreciated.
Regards,
John

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Advanced Filter Error

Post by HansV »

Combining two CI_STARTSWITH conditions with AND doesn't make sense: an item cannot start with two different strings at once.
Apart from that, I cannot make it work. If I copy a valid condition from the SQL tab of the Filter window and paste it into VBA (doubling the quotes), I still get an error.

I'd ask this question in Microsoft's Outlook for Developers forum. You'll probably get help from Outlook MVPs there.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: VBA Advanced Filter Error

Post by jstevens »

Hans,

I was able to develop an alternate solution based on a posting found in the Outlook for Developers forum.

Thanks for pointing me to the forum.
Regards,
John

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Advanced Filter Error

Post by HansV »

Good to hear that.
Best wishes,
Hans