首页 文章

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

提问于
浏览
2

我正在使用excel-vba和outlook进行项目 .

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

  • 检查是否有未读电子邮件,
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

如果有未读电子邮件,

  • 我需要检查这些未读电子邮件中是否有附件 .

如果有附件,

  • 我需要检查这些附件是否有附件名称,其中包含“ 生产环境 计划”作为名称的一部分 .

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

附件名称将以这种方式

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

  • 如果有这样的附件,那么应该在excel中显示一个MsgBox

消息框“此类附件可用”

在这个时间点我知道如何做第1和第4部分 .

我想知道:如何做第2部分和第3部分?

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

更新:我做了一个小的补充,这是行不通的 . 这是为了显示一个msg,如果检测到附件,但它们不是“ 生产环境 计划”的形式 .

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

1 回答

  • 2

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

    EDIT - 列出所有附件

    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

相关问题