Warm tip: This article is reproduced from stackoverflow.com, please click
outlook vba outlook-vba outlook-filter

Item.Restrict Function doesn't work when using like instead of =

发布于 2020-03-27 10:29:58

I'm creating a macro that pulls e-mail attachments into a folder to be opened and copied, which works 100% perfect when the subject name is predefined, i.e. never changes.

' this works
Set oOlInbFiltered = oOlInb.Items.Restrict("[Subject] = " & SubjectName)

However, when I try to instead restrict for a preset beginning, say every email begins with 'aaaaa', it causes an automation error with the code below:

' this doesnt
Set oOlInbFiltered = oOlInb.Items.Restrict("[Subject] like '" & PrefixName & "%'")

Any help?

Expected results: No error message, files in folder. Instead I receive

Run-time error '-2147352567 (800200009)': Automation error Exception Occurred.

Questioner
Matthew Dallow
Viewed
146
Eugene Astafiev 2019-07-04 05:01

Try to use the following code instead:

criteria = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '" & PrefixName & "%'" 
Set oOlInbFiltered = oOlInb.Items.Restrict(criteria)