Outlook VBA脚本使用电子邮件正文中的信息自动保存附件以命名文件

时间:2018-10-09 15:20:24

标签: outlook rename attachment autosave

需要使用VBA脚本来自动保存附件,但需要使用电子邮件正文中的信息来命名保存的文件。电子邮件示例:

送货信息:

消息号:246439839

本地电话:2395945852 远程CSID:2399318665 远程CID:2392780330 总页数:1

接收时间:美国太平洋夏令时间6/8/2016 7:09:50 传输时间:32.000秒

文件名将是: NM-246439839-LN-2395945852-CSID-2399318665-CID-2392780330.pdf

文件始终为PDF格式

本地号码,远程CSID和远程CID并不总是填充的(这就是为什么我需要所有这些都能够使用填充的任何一个进行过滤)。

消息#必须是文件名的一部分,以确保每个文件的唯一名称。

电子邮件为HTML格式,并且始终相同,因此也许可以使用标签,如果需要,我可以发布HTML。

我已经创建了监视文件夹的脚本,然后根据文件名将文件排序/移动到适当的位置。电子邮件来自我们的新传真提供商,他们无法像我们以前的系统一样将这些电子邮件添加到主题行。

当前使用:

Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
    saveFolder = "c:\Data\Fax"
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
        Set objAtt = Nothing
    Next
End Sub

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

仅参考示例代码,您可以根据需要对其进行更新:

Public p As String 'File Save location, also unzip the file storage location
Public Sub SaveAttach(Item As Outlook.MailItem)
    p = "C:\Users\Administrator.TXV6HLXTU3ZW8KD\Desktop\"
    SaveAttachment Item, p, "*.rar"  'Here *.rar can be changed to other regular expressions
    ' MsgBox "File saved."
End Sub

' save file
' path is the save path, condition is the attachment name match condition
Private Sub SaveAttachment(ByVal Item As Object, path$, Optional condition$ = "*")
    Dim olAtt As Attachment
    Dim i As Integer
    Dim m As Long
    Dim s As String
    If Item.Attachments.Count > 0 Then
        For i = 1 To Item.Attachments.Count
            Set olAtt = Item.Attachments(i)
            If olAtt.FileName Like condition Then
                olAtt.SaveAsFile path & olAtt.FileName
        'The following section is to extract the rar file, p is the save location
                s = "C:\Program Files\WinRAR\WinRAR.exe" & " X " & path & olAtt.FileName & " " & p 'Note Find the Decompression software location
                m = Shell(s, vbHide)
            End If
        Next
    End If
    Set olAtt = Nothing
End Sub

然后在Outlook中创建新规则,选择执行脚本的操作,选择此方法的脚本,您将收到自动保存附件并提取到桌面的消息。