【问题标题】:Exporting new email to file将新电子邮件导出到文件
【发布时间】:2020-06-21 08:24:26
【问题描述】:

我们会从几个来源收到特定的电子邮件。对它们进行分类的最简单方法是按邮件标题甚至源电子邮件地址。

我们正在尝试自动将所有收到的电子邮件保存到文件中,无论是 TXT 还是 PDF,以便在网络、电子邮件或其他任何故障出现问题时提取备份文件。

我尝试从几个类似的主题创建一个宏;

Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        SaveMailAsFile Item ' call sub
    End If
End Sub

Public Sub SaveMailAsFile(ByVal Item As Object)

    Dim olNs As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim SubFolder As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim ItemSubject As String
    Dim NewName As String
    Dim RevdDate As Date
    Dim Path As String
    Dim Ext As String
    Dim i As Long

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items.Restrict("[Subject] = 'VVAnalyze Results'")

    Path = Environ("USERPROFILE") & "\Desktop\Backup Reports\"
    ItemSubject = Item.Subject
    RevdDate = Item.ReceivedTime
    Ext = "txt"

    For i = Items.Count To 1 Step -1
        Set Item = Items.Item(i)

        DoEvents

        If Item.Class = olMail Then
            Debug.Print Item.Subject ' Immediate Window
            Set SubFolder = Inbox.Folders("Temp") ' <--- Update Fldr Name

            ItemSubject = Format(RevdDate, "YYYYMMDD-HHNNSS") _
                                                    & " - " & _
                                            Item.Subject & Ext

            ItemSubject = FileNameUnique(Path, ItemSubject, Ext)

            Item.SaveAs Path & ItemSubject, olTXT
            Item.Move SubFolder
        End If
    Next

    Set olNs = Nothing
    Set Inbox = Nothing
    Set SubFolder = Nothing
    Set Items = Nothing

End Sub


'// Check if the file exists
Private Function FileExists(FullName As String) As Boolean
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(FullName) Then
        FileExists = True
    Else
        FileExists = False
    End If

    Exit Function
End Function

'// If the same file name exist then add (1)
Private Function FileNameUnique(Path As String, _
                               FileName As String, _
                               Ext As String) As String
Dim lngF As Long
Dim lngName As Long
    lngF = 1
    lngName = Len(FileName) - (Len(Ext) + 1)
    FileName = Left(FileName, lngName)

    Do While FileExists(Path & FileName & Chr(46) & Ext) = True
        FileName = Left(FileName, lngName) & " (" & lngF & ")"
        lngF = lngF + 1
    Loop

    FileNameUnique = FileName & Chr(46) & Ext

    Exit Function
End Function

虽然我知道 Outlook 缓存即使离线也可用,但有些人坚持要在物理硬盘上备份文件。

我知道我可以手动选择这些文件并通过拖放创建副本,但这还不够。

我知道 https://www.techhit.com/messagesave/screenshots.html。这个想法很难被接受,因为 GDPR 等等等等。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您可以使用此代码,将其粘贴到 ThisOutlookSession 模块中。

    要在不重新启动 Outlook 的情况下测试此代码示例,请单击 Application_Startup 过程,然后单击运行。

    Option Explicit
    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
      Dim Ns As Outlook.NameSpace
      Set Ns = Application.GetNamespace("MAPI")
      Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
      If TypeOf Item Is Outlook.MailItem Then
    
      Dim sPath As String
      Dim dtDate As Date
      Dim sName As String
      Dim enviro As String
    
      enviro = CStr(Environ("USERPROFILE"))
    
      sName = Item.Subject
      ReplaceCharsForFileName sName, "_"
    
      dtDate = Item.ReceivedTime
      sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
        vbUseSystem) & Format(dtDate, "-hhnnss", _
        vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
    
    ' use My Documents for older Windows.
        sPath = enviro & "\Documents\"
      Debug.Print sPath & sName
      Item.SaveAs sPath & sName, olMSG
    
      End If
    
    End Sub
    
    Private Sub ReplaceCharsForFileName(sName As String, _
      sChr As String _
    )
      sName = Replace(sName, "/", sChr)
      sName = Replace(sName, "\", sChr)
      sName = Replace(sName, ":", sChr)
      sName = Replace(sName, "?", sChr)
      sName = Replace(sName, Chr(34), sChr)
      sName = Replace(sName, "<", sChr)
      sName = Replace(sName, ">", sChr)
      sName = Replace(sName, "|", sChr)
    End Sub
    

    更多信息,请参考此链接:

    Save all incoming messages to the hard drive

    Save outlook mail automatically to a specified folder

    【讨论】:

    • 我正在努力解决这个问题,但无论我做什么以及如何运行代码似乎都没有做任何事情。我设法让它至少在下面的行中给我一个错误; 'Item.SaveAs sPath & sName, olMSG' 它给了我运行时错误'-21447286788 (800300fc)':操作失败。我已经修改了默认文件夹位置,所以我首先尝试一些通用的东西,例如 c:\test\ 我不知道如何或多或少地使用代码......我的意思是我知道命令在做什么但是我很确定问题可能只是因为我没有足够的编码知识。
    • 我已经弄明白了(代码中有文件路径错误)!这是天才!太有趣了 非常感谢你!快速提问:根据我的理解 enviro = CStr(Environ("USERPROFILE")) 让 VB 知道我们在某些用户文件中工作对吗?我知道我实际上可以在该命令中输入整个目的地。我还可以限制此代码仅对某些电子邮件组作出反应吗?例如,我们收到大量标题为 NEW SALE 或 NEW PURCHASE 的电子邮件,我可以使用这个事实让 VB 只对这些邮件做出反应吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2011-01-18
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多