2015-03-25 67 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属性。