Redemption-Outlook VBA脚本用于从特定发件人移动带有附加的msg文件的邮件

时间:2018-02-17 09:48:13

标签: vba outlook outlook-vba outlook-redemption mailitem

我正在尝试使用Redemption为Outlook编写VB脚本。我的任务如下:

  1. 循环浏览收件箱中的所有电子邮件
  2. 在我浏览时检查每封邮件
  3. 如果有附件我想进一步检查
  4. 如果附件是msg文件且来自某个发件人,则将其移至特定文件夹
  5. 我已经确定Redemption是最容易使用的,因为它允许您检查附件而无需保存并打开它们。我有以下工作,它将告诉我所选电子邮件附加信息的信息。

    $stateProvider.state('myState', {
        parent: 'baseState',
        url: '/calendar?firstAvailableDate',
        template: 'calendar.html',
        controller: 'CalendarController',
        controllerAs: 'calendarCtrl',
        redirectTo: (trans) => {
            if (trans.params().firstAvailableDate === '') {
                var CalendarService = trans.injector().get('CalendarService');
                return CalendarService.getAvailableDates().then(function(response){
                    return { state: 'myState', params: { firstAvailableDate: response[0] }};
                });
            }
        }
    });
    

    我还没有找到一种方法来通过我的收件箱中的每个项目。但是我已经能够通过收件箱中的电子邮件循环来获取主题。

    Sub GetAttachmentInfo()
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    
    Dim olNS As Outlook.NameSpace
    Set olNS = olApp.GetNamespace("MAPI")
    Dim FolderSrc As MAPIFolder
    Set FolderSrc = CreateObject("Outlook.Application"). _
                  GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    
    Dim oRDOSession As Redemption.RDOSession
    Set oRDOSession = CreateObject("Redemption.RDOSession")
    oRDOSession.MAPIOBJECT = olNS.Application.Session.MAPIOBJECT
    
    Set Inbox = oRDOSession.GetDefaultFolder(olFolderInbox)
    
    Set Mail = olApp.ActiveExplorer.Selection.Item(1)
    Debug.Print "EntryID: " & Mail.EntryID
    Set Mail = oRDOSession.GetMessageFromID(Mail.EntryID)
    For Each Msg In FolderSrc.Items
        For Each att In Mail.Attachments
        Debug.Print "Sender: " & att.EmbeddedMsg.SenderEmailAddress
        Debug.Print "Embedded Msg Subject: " & att.EmbeddedMsg
        Next
    Next
    End Sub
    

    我知道基本想法只是一个

    Sub subjectLine()
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    Dim Folder As Object
    Dim olNS As Outlook.NameSpace
    Set olNS = olApp.GetNamespace("MAPI")
    Dim oRDOSession As Redemption.RDOSession
    Set oRDOSession = CreateObject("Redemption.RDOSession")
    oRDOSession.MAPIOBJECT = olNS.Application.Session.MAPIOBJECT
    
    
    Set Folder = Session.GetDefaultFolder(olFolderInbox)
    For Each Msg In Folder.Items
      Debug.Print (Msg.Subject)
    Next
    End Sub
    

    有人可以就如何做到这一点提出建议吗?

1 个答案:

答案 0 :(得分:0)

在您的第一个脚本中,您已经遍历收件箱中的所有项目,但您从未触摸过它们 - 您正在不断处理所选邮件的附件。在行

 For Each att In Mail.Attachments

用Msg替换Mail。您还需要确保触摸嵌入的邮件附件。在我的头顶:

For Each Msg In FolderSrc.Items
    For Each att In Msg .Attachments
      if att.Type = 5 Then 'olEmbeddedItem
        set embeddedMsg = att.EmbeddedMsg
        Debug.Print "Sender: " & embeddedMsg.SenderEmailAddress
        Debug.Print "Embedded Msg Subject: " & embeddedMsg.Subject
      End If
    Next
Next