Outlook .items.restrict使用两个过滤器 [英] Outlook .items.restrict using two filters

查看:504
本文介绍了Outlook .items.restrict使用两个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个打开电子邮件并下载其附件的脚本。现在我可以选择在最近的电子邮件中下载最新的附件:

  Sub CTEmailAttDownload()

Const olFolderInbox As Integer = 6
'~~>附件路径
Const AttachmentPath As String =C:\TEMP\TestExcel

Dim oOlAp As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim oOlItm As Object
Dim oOlAtch As Object
Dim oOlResults As Object

Dim x As Long

Dim NewFileName As String
NewFileName =Daily Tracker&格式(现在,dd-MM-yyyy)

'你只能有一个单一的Outlook实例,所以如果它已经打开
'这将与GetObject相同,否则它会打开Outlook。
设置oOlAp = CreateObject(Outlook.Application)
设置oOlns = oOlAp.GetNamespace(MAPI)
设置oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

从昨天开始,搜索整个收件箱就没有意义。
设置oOlResults = oOlInb.Items.Restrict([ReceivedTime]>'&格式(日期 - 1,DDDDD HH:NN)&')

如果你有一个以上的附件,他们将会相互覆盖。
'x将更新文件名。
x = 1
对于每个oOlItm在oOlResults
如果oOlItm.Attachments.Count> 0然后
对于每个oOlAtch在oOlItm.Attachments
如果GetExt(oOlAtch.FileName)=xlsx然后
oOlAtch.SaveAsFile AttachmentPath& \& NewFileName& .xlsx
End If
x = x + 1
Next oOlAtch
End If
Next oOlItm

End Sub

'--------------------------------------------- -------------------------
'GetExt
'
'返回文件的扩展名。
'--------------------------------------------- -------------------------
公共函数GetExt(FileName As String)As String

Dim mFSO As对象
设置mFSO = CreateObject(Scripting.FileSystemObject)

GetExt = mFSO.GetExtensionName(FileName)
结束函数
/ pre>

使用 '[主题] =' 我可以下载它学科。



我的问题是,如何将这两个过滤器放在一起,以便我可以通过Subject和ReceivedTime进行过滤?



我尝试用 & + ,到目前为止我还没有成功。

解决方案

甚至一个语法限制。如Scott Holtzman的评论所示,如果您分别了解每个过滤器,可以过滤两次。

  Option Explicit 

Sub CTEmailAttDownload()

Const olFolderInbox As Integer = 6
'~~>附件路径
Const AttachmentPath As String =C:\TEMP\TestExcel

Dim oOlAp As Object
Dim oOlns As Object
Dim oOlInb As Object

Dim oOlItm As Object
Dim oOlAtch As Object

Dim oOlResults As Object
Dim oOlSubjectResults As Object
Dim strFilter As String
Dim i As Long

Dim x As Long

Dim NewFileName As String
NewFileName =Daily Tracker&格式(现在,dd-MM-yyyy)

'你只能有一个单一的Outlook实例,所以如果它已经打开
'这将与GetObject相同,否则它会打开Outlook。
设置oOlAp = CreateObject(Outlook.Application)
设置oOlns = oOlAp.GetNamespace(MAPI)
设置oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

从昨天开始,搜索整个收件箱就没有意义。
设置oOlResults = oOlInb.Items.Restrict([ReceivedTime]>'& format(Date - 1,DDDDD HH:NN)&')

strFilter =@ SQL =& Chr(34)& urn:schemas:httpmail:subject& Chr(34)& like'%test%'

设置oOlSubjectResults = oOlResults.Restrict(strFilter)

如果oOlSubjectResults.count = 0然后
Debug.Print没有电子邮件发现与适用的主题

Else
'如果您有多个单独的附件,他们将相互覆盖。
'x将更新文件名。
x = 1

对于i = 1 To oOlSubjectResults.count
设置oOlItm = oOlSubjectResults(i)
如果oOlItm.Attachments.count> 0然后
Debug.Print oOlItm.Subject
对于每个oOlAtch在oOlItm.Attachments

Debug.Print oOlAtch.DisplayName

如果GetExt(oOlAtch。 FileName)=xlsx然后
oOlAtch.SaveAsFile AttachmentPath& \& NewFileName& .xlsx
结束如果
x = x + 1
下一个oOlAtch
结束如果
下一个i
如果

ExitRoutine:
Set oOlAp = Nothing
Set oOlns = Nothing
Set oOlInb = Nothing

设置oOlResults = Nothing
设置oOlSubjectResults = Nothing

End Sub


I'm using a script that opens an email and downloads its attachment. Right now I can either choose to download the most recent attachment on the most recent email:

Sub CTEmailAttDownload()

Const olFolderInbox As Integer = 6
'~~> Path for the attachment
Const AttachmentPath As String = "C:\TEMP\TestExcel"

    Dim oOlAp As Object
    Dim oOlns As Object
    Dim oOlInb As Object
    Dim oOlItm As Object
    Dim oOlAtch As Object
    Dim oOlResults As Object

    Dim x As Long

    Dim NewFileName As String
    NewFileName = "Daily Tracker " & Format(Now, "dd-MM-yyyy")

    'You can only have a single instance of Outlook, so if it's already open
    'this will be the same as GetObject, otherwise it will open Outlook.
    Set oOlAp = CreateObject("Outlook.Application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

    'No point searching the whole Inbox - just since yesterday.
    Set oOlResults = oOlInb.Items.Restrict("[ReceivedTime]>'" & Format(Date - 1, "DDDDD HH:NN") & "'")

    'If you have more than a single attachment they'll all overwrite each other.
    'x will update the filename.
    x = 1
    For Each oOlItm In oOlResults
        If oOlItm.Attachments.Count > 0 Then
            For Each oOlAtch In oOlItm.Attachments
                If GetExt(oOlAtch.FileName) = "xlsx" Then
                    oOlAtch.SaveAsFile AttachmentPath & "\" & NewFileName & ".xlsx"
                End If
                x = x + 1
            Next oOlAtch
        End If
    Next oOlItm

End Sub

'----------------------------------------------------------------------
' GetExt
'
'   Returns the extension of a file.
'----------------------------------------------------------------------
Public Function GetExt(FileName As String) As String

    Dim mFSO As Object
    Set mFSO = CreateObject("Scripting.FileSystemObject")

    GetExt = mFSO.GetExtensionName(FileName)
End Function

By using '[Subject] =' I can download it by subject.

My question is, how can I put those two filters together so I can filter by Subject and ReceivedTime?

I tried binding them together with ,, &, + and so far I haven't been successful.

解决方案

It is a struggle to get the syntax for even one restrict. As indicated in the comment by Scott Holtzman, if you know each filter separately, you can filter twice.

Option Explicit

Sub CTEmailAttDownload()

    Const olFolderInbox As Integer = 6
    '~~> Path for the attachment
    Const AttachmentPath As String = "C:\TEMP\TestExcel"

    Dim oOlAp As Object
    Dim oOlns As Object
    Dim oOlInb As Object

    Dim oOlItm As Object
    Dim oOlAtch As Object

    Dim oOlResults As Object
    Dim oOlSubjectResults  As Object
    Dim strFilter As String
    Dim i As Long

    Dim x As Long

    Dim NewFileName As String
    NewFileName = "Daily Tracker " & format(Now, "dd-MM-yyyy")

    'You can only have a single instance of Outlook, so if it's already open
    'this will be the same as GetObject, otherwise it will open Outlook.
    Set oOlAp = CreateObject("Outlook.Application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

    'No point searching the whole Inbox - just since yesterday.
    Set oOlResults = oOlInb.Items.Restrict("[ReceivedTime]>'" & format(Date - 1, "DDDDD HH:NN") & "'")

    strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%test%'"

    Set oOlSubjectResults = oOlResults.Restrict(strFilter)

    If oOlSubjectResults.count = 0 Then
        Debug.Print "No emails found with applicable subject"

    Else
        'If you have more than a single attachment they'll all overwrite each other.
        'x will update the filename.
        x = 1

        For i = 1 To oOlSubjectResults.count
            Set oOlItm = oOlSubjectResults(i)
            If oOlItm.Attachments.count > 0 Then
                Debug.Print oOlItm.Subject
                For Each oOlAtch In oOlItm.Attachments

                    Debug.Print oOlAtch.DisplayName

                    If GetExt(oOlAtch.FileName) = "xlsx" Then
                        oOlAtch.SaveAsFile AttachmentPath & "\" & NewFileName & ".xlsx"
                    End If
                    x = x + 1
                Next oOlAtch
            End If
        Next i
    End If

ExitRoutine:
    Set oOlAp = Nothing
    Set oOlns = Nothing
    Set oOlInb = Nothing

    Set oOlResults = Nothing
    Set oOlSubjectResults = Nothing

End Sub

这篇关于Outlook .items.restrict使用两个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆