使用VBA在Outlook 2010中打开Excel附件

时间:2015-03-25 10:33:42

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

我想知道如何使用VBA代码在收件箱Outlook 2010中打开Excel附件。

我希望代码能够:

  1. 检查未更改“测试”的特定主题
  2. 检查该电子邮件是否已读或未读,以及是否未读取该电子邮件
  3. 我有一条规则,根据主题将电子邮件存储在子文件夹中,但我可以将其更改为返回主收件箱

    如果您能解释代码的作用,我将非常感激,因为我不熟悉Outlook连接位。


    这是我从各个站点汇总的内容,包括stackoverflow,它完成了这项工作。

    • 似乎在10PM和5PM电子邮件的主题中运行具有RE的电子邮件。我明确地命名了两封电子邮件的主题。谁能帮我?
    • 我不熟悉为Outlook对象声明变量。我是否正确声明了变量?
    • 我还想复制每个文件中的数据并将其粘贴到另一个工作簿中。我希望将数据粘贴到每个工作簿的第一个数据下面,这样就需要找到最后一行并将每个工作簿上每个数据最后一行下面的一行粘贴到打开的工作簿上,该工作簿存储在另一个路径中

    Sub DownloadAttachmentFirstUnreadEmail()
    
        Const olFolderInbox = 6
        Const AttachmentPath As String = "C:\My Documents\Outlook Test\"
    
        Dim oOlAtch As Object
    
        Set objOutlook = CreateObject("Outlook.Application")
        Set objNamespace = objOutlook.GetNamespace("MAPI")
        Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
        Set objFolder = objFolder.Folders("**CLIENT ISSUES**").Folders("*Daily Reports").Folders("1. Open Trade Report")
    
        Set colItems = objFolder.Items
        Set colFilteredItems1 = colItems.Restrict("[Unread] = True AND [Subject] = '10PM FXC Email notification for Martin Currie'")
        Set colFilteredItems2 = colItems.Restrict("[Unread] = True AND [Subject] = 'FXC Email notification for Martin Currie Funds'")
    
           '~~> Check if there are any actual unread 10PM FXC emails
        If colFilteredItems1.Count = 0 Then
            MsgBox "NO Unread 10PM Email In Inbox"
        Else
            '~~> Extract the attachment from the 1st unread email
            For Each colItems In colFilteredItems1
                '~~> Check if the email actually has an attachment
                If colItems.Attachments.Count <> 0 Then
                    For Each oOlAtch In colItems.Attachments
                        '~~> save the attachment and open them
                        oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
                        Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
                    Next oOlAtch
                Else
                    MsgBox "10PM email doesn't have an attachment"
                End If
            Next colItems
    
        End If
    
               '~~> Check if there are any actual unread FXC Email emails
        If colFilteredItems2.Count = 0 Then
            MsgBox "NO Unread 5PM Email In Inbox"
        Else
            '~~> Extract the attachment from the 1st unread email
            For Each colItems In colFilteredItems2
                '~~> Check if the email actually has an attachment
                If colItems.Attachments.Count <> 0 Then
                    For Each oOlAtch In colItems.Attachments
                        '~~> save the attachment and open them
                        oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
                        Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
                    Next oOlAtch
                Else
                    MsgBox "5PM email doesn't have an attachment"
                End If
            Next colItems
    
        End If
    
    End Sub
    

1 个答案:

答案 0 :(得分:1)

首先,我建议从MSDN中的Getting Started with VBA in Outlook 2010文章开始。

您可以将VBA宏分配给Outlook规则,它应如下所示:

public sub test(mail as MailItem)
    ' 
end sub

您可以在其中查看邮件对象。看起来您需要查看MailItem类的SubjectUnReadAttachments属性。