2015-03-25 103 views
-1

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

我想代碼:

  1. 檢查特定的主題不會改變「測試」
  2. 檢查電子郵件已讀或未讀,如果是讀就用它

我有一個適當的規則,其中電子郵件存儲在基於主題的子文件夾,但我可以改變它返回到主收件箱

我真的APPR eciate,如果你可以解釋代碼在做什麼,因爲我不熟悉Outlook連接位。


這就是我從各個網站拉到一起,包括stackoverflow它做的工作。

  • 它似乎運行的電子郵件有RE:在主題爲10PM和5PM電子郵件。我明確命名了這兩封電子郵件的主題。誰能幫我?
  • 我不熟悉爲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

首先,我建議從Getting Started with VBA in Outlook 2010文章開始在MSDN。

您可以將VBA宏到Outlook規則,它應該看起來像下列之一:

public sub test(mail as MailItem) 
    ' 
end sub 

在那裏你可以檢查出的郵件對象。看起來您需要查看MailItem類的Subject,UnReadAttachments屬性。