使用Inbox.Items.Restrict

时间:2017-04-02 07:01:00

标签: vba excel-vba outlook outlook-vba outlook-filter

我正在编写一个代码,用于自动从未读邮件中下载包含主题中几个关键字的附件,例如“training”,但是当我尝试使用SQL查询查看未读邮件时,它给了我一个错误。

  Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
               Chr(34) & " Like '%Training%' AND " & _
               Chr(34) & "urn:schemas:httpmail:hasattachment" & _
               Chr(34) & "=1"
  Set Items = Inbox.Items.Restrict(Filter) 'No error while running this code
 Filter = "@SQL=" & Chr(34) & "& Chr(34) & "urn:schemas:httpmail:subject" & _
               Chr(34) & " Like '%Training%' AND" & _
                    Chr(34) & "urn:schemas:httpmail:hasattachment" & _
                    Chr(34) & "= 1" & Chr(34) & "AND" & _
                    Chr(34) & "urn:schemas:httpmail:read" & _
                    Chr(34) & "= 0"


Set Items = Inbox.Items.Restrict(Filter) 
' Now here it is giving me runtime error '-2147352567(800200009)'

所有帮助将不胜感激。在此先感谢

1 个答案:

答案 0 :(得分:1)

你几乎得到了它,

错误来自此处 Chr(34) & "= 1" & Chr(34) & "AND" & _

应该 Chr(34) & "=1 AND " & _

实施例

Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
                   Chr(34) & " Like '%Training%' AND " & _
                   Chr(34) & "urn:schemas:httpmail:hasattachment" & _
                   Chr(34) & "=1 AND " & _
                   Chr(34) & "urn:schemas:httpmail:read" & _
                   Chr(34) & "=0"
  DASL过滤器支持的

Filtering Items Using a String Comparison包括等价,前缀,短语和子串匹配。请注意,当您对Subject属性进行过滤时,前缀如" RE:"和" FW:"被忽略了。