簡體   English   中英

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

[英]Open an Excel attachment in Outlook 2010 using VBA

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

我希望代碼:

  1. 檢查不改變“測試”的特定主題
  2. 檢查該電子郵件是否已讀或未讀,以及是否未讀以使用該電子郵件

我有一個規則,根據主題將電子郵件存儲在子文件夾中,但我可以更改它以返回主收件箱

如果您能解釋代碼在做什么,我將不勝感激,因為我不熟悉 Outlook 連接位。


這是我從各種站點(包括 stackoverflow)中提取的內容,它可以完成這項工作。

  • 它似乎適用於在 10PM 和 5PM 電子郵件的主題中包含 RE: 的電子郵件。 我明確命名了兩封電子郵件的主題。 誰能幫我?
  • 我不熟悉為 Outlook 對象聲明變量。 我是否正確聲明了變量?
  • 我還想復制每個文件中的數據並將其粘貼到另一個工作簿中。 我希望將數據粘貼在每個工作簿的第一個數據下方,因此需要找到最后一行並將每個工作簿上的每個數據的最后一行下方的 1 行粘貼到存儲在另一個路徑中的打開工作簿上.

.

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

首先,我建議從 MSDN 中 Outlook 2010中的VBA 入門文章開始

您可以為 Outlook 規則分配一個 VBA 宏,它應該如下所示:

public sub test(mail as MailItem)
    ' 
end sub

您可以在其中查看郵件對象。 看起來您需要檢查 MailItem 類的SubjectUnReadAttachments屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM