检查是否有未读邮件,附件名称包含 "Production_Plan" 作为名称的一部分,使用 excel - vba

Check if there are unread emails, with an attachment name containing "Production_Plan" as part of the name ,using excel - vba

我正在使用 excel-vba 和 outlook 进行一个项目。

我正在使用 excel 工作簿。我需要能够 运行 一个宏,以便:

  1. 检查是否有未读邮件,

    Dim oOutlook As Object
    Dim oOlns As Object
    Dim oOlInb As Object
    
    Const olFolderInbox = 6
    
    '~~> Get Outlook instance
    Set oOutlook = GetObject(, "Outlook.application")
    Set oOlns = oOutlook.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    
    '~~> Check if there are any actual unread emails
    If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
    
        MsgBox "NO Unread Email In Inbox"
    
         Else
    
          MsgBox "Unread Email available In Inbox"
    
    Exit Sub
    

    如果有未读邮件,

  2. 我需要检查这些未读邮件中是否有附件。

    如果有附件,

  3. 我需要检查这些附件的附件名称是否包含 "Production Plan" 作为名称的一部分。

这是因为这个附件是定期发送给我的。

附件名称将采用这种方式

生产计划(日-月-年).xls

  1. 如果有这样的附件那么MsgBox应该显示在excel说

    消息框"Such attachments are available"

此时我知道如何完成第 1 部分和第 4 部分。

我想知道:第 2 部分和第 3 部分怎么做?

请指导我如何做到这一点。

update:我做了一个小的补充,没有用。这是为了在检测到附件时显示消息,但它们不是 "Production Plan".

的形式
Else
      If Not att.Filename Like "Production Plan*.xls" Then
            MsgBox "no production plan attachment"
    Exit Sub
End If

我没有 Outlook,所以未经测试:

编辑 - 列出所有附件

Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
Dim some As String, other As String

Const olFolderInbox = 6

'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")

If unRead.Count = 0 Then
    MsgBox "NO Unread Email In Inbox"
Else

    some = ""
    other = ""

    For Each m In unRead
        If m.Attachments.Count > 0 Then
            For Each att In m.Attachments
                If att.Filename Like "Production Plan*.xls" Then
                    some = some & vbLf & "  -  " & att.Filename
                Else
                    other = other & vbLf & "  -  " & att.Filename
                End If
            Next att
        End If
    Next m


    If some <> "" Or other <> "" Then
        MsgBox "Production Plans:" & vbLf & _
               IIf(some <> "", some, "{none}") & _
               vbLf & vbLf & "Other files:" & vbLf & _
               IIf(other <> "", other, "{none}"), _
               vbExclamation, "Unread mails with attachments!"

    End If


End If

您可能会发现来自 Siddharth Rout 的这个庞大答案很有用:Download attachment from Outlook and Open in Excel