简体   繁体   English

VBA代码,用于将附件(excel文件)保存在另一封电子邮件中作为附件的Outlook电子邮件中

[英]VBA Code to save an attachment (excel file) from an Outlook email that was inside another email as an attachment

I have code that saves attachments in message in a specific Outlook folder. 我有代码保存特定Outlook文件夹中的邮件附件。

My script will work if the email has an attachment, but will not work if the email was sent as an attachment with an attachment . 如果电子邮件包含附件,我的脚本将起作用,但如果电子邮件作为带附件的附件发送,则无法使用

In this case my emails contains other emails as attachments (from an auto-forward rule). 在这种情况下,我的电子邮件包含其他电子邮件作为附件(来自自动转发规则)。 The embedded email attachments then contain excel files. 嵌入的电子邮件附件随后包含excel文件。

Please see my current below: 请看下面我目前的

Public Sub SaveOlAttachments()
  Dim isAttachment As Boolean
  Dim olFolder As Outlook.MAPIFolder
  Dim msg As Outlook.MailItem
  Dim att As Outlook.Attachment
  Dim fsSaveFolder, sSavePathFS, ssender As String

  On Error GoTo crash

  fsSaveFolder = "C:\Documents and Settings\user\Desktop\"
  isAttachment = False
  Set olFolder = Outlook.GetNamespace("MAPI").Folders("...email server...")
  Set olFolder = olFolder.Folders("Inbox")
  If olFolder Is Nothing Then Exit Sub

  For Each msg In olFolder.Items
    If UCase(msg.Subject) = "TEST EMAIL WITH ATTACHMENT" Then
                    If msg.Attachments.Count > 0 Then
          While msg.Attachments.Count > 0
                sSavePathFS = fsSaveFolder & msg.Attachments(1).Filename
            msg.Attachments(1).SaveAsFile sSavePathFS
            msg.Attachments(1).Delete
            isAttachment = True
          Wend
          msg.Delete
        End If
    End If    
  Next

crash:
  If isAttachment = True Then Call findFiles(fsSaveFolder)
End Sub

Any help would be much appreciated. 任何帮助将非常感激。

The code below uses this approach to work on the email as an attachment 下面的代码使用此方法作为附件处理电子邮件

  1. Tests whether the attachment is an email message or not (if the filename ends in msg) 测试附件是否是电子邮件消息(如果文件名以msg结尾)
  2. If the attachment is a message, it is saved as "C:\\temp\\KillMe.msg" . 如果附件是消息,则将其保存为"C:\\temp\\KillMe.msg"
  3. CreateItemFromTemplate is used to access the saved file as a new message (msg2) CreateItemFromTemplate用于将保存的文件作为新消息访问(msg2)
  4. The code then processes this temporary message to strip the attachmnets to fsSaveFolder 然后代码处理此临时消息以将attachmnets剥离到fsSaveFolder
  5. If the attachment is not a message then it is extracted as per your current code 如果附件不是消息,则根据您当前的代码提取它

Note that as I didnt have your olFolder structure, Windoes version, Outlook variable etc I have had to add in my own file paths and Outlook folders to test. 请注意,因为我没有你的olFolder结构,Windoes版本, Outlook变量等我必须添加我自己的文件路径和Outlook文件夹进行测试。 You will need to change these 您需要更改这些

   Sub SaveOlAttachments()

    Dim olFolder As Outlook.MAPIFolder
    Dim msg As Outlook.MailItem
    Dim msg2 As Outlook.MailItem
    Dim att As Outlook.Attachment
    Dim strFilePath As String
    Dim strTmpMsg As String
    Dim fsSaveFolder As String

    fsSaveFolder = "C:\test\"

    'path for creating attachment msg file for stripping
    strFilePath = "C:\temp\"
    strTmpMsg = "KillMe.msg"

   'My testing done in Outlok using a "temp" folder underneath Inbox
    Set olFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set olFolder = olFolder.Folders("Temp")
    If olFolder Is Nothing Then Exit Sub

    For Each msg In olFolder.Items
        If msg.Attachments.Count > 0 Then
        While msg.Attachments.Count > 0
        bflag = False
            If Right$(msg.Attachments(1).FileName, 3) = "msg" Then
                bflag = True
                msg.Attachments(1).SaveAsFile strFilePath & strTmpMsg
                Set msg2 = Application.CreateItemFromTemplate(strFilePath & strTmpMsg)
            End If
            If bflag Then
                sSavePathFS = fsSaveFolder & msg2.Attachments(1).FileName
                msg2.Attachments(1).SaveAsFile sSavePathFS
                msg2.Delete
            Else
                sSavePathFS = fsSaveFolder & msg.Attachments(1).FileName
                msg.Attachments(1).SaveAsFile sSavePathFS
            End If
            msg.Attachments(1).Delete
            Wend
             msg.Delete
        End If
    Next
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Outlook 中通过文件保存电子邮件附件 - Save Email Attachment Over File From Outlook Outlook VBA将电子邮件及其附件保存在草稿中 - Outlook VBA save email with attachment in the draft VBA Outlook Email 附件保存权限 - VBA Outlook Email Attachment Save Permissions 使用 Excel VBA 发送带有附件的 Outlook 电子邮件 - Send Outlook email with attachment using Excel VBA 在 Outlook 中仅保存电子邮件中的附件 - Save only attachment from email in outlook Excel Macro以保存Outlook 2010附件,将最早的电子邮件保存为最新的电子邮件 - Excel Macro to Save Outlook 2010 attachment, oldest email to newest email Outlook VBA从邮件中保存附件,然后将附件数据复制到另一个excel中并通过邮件发送发送excel - Outlook VBA to save attachment from a mail,and then copy the attachment data in another excel and send the send excel via mail 自动从电子邮件下载附件并将其保存到 Excel - Download and Save Attachment from Email Automatically to Excel 使用 Excel Z6E3EC7E6A9F6007B0838FC0EEZ9A 在主题和附件中查找 Outlook Email 关键字 - Find Outlook Email with keywords in Subject & attachment using Excel VBA Outlook-从带有.xls附件的电子邮件和特定发件人中保存文件,然后将电子邮件移至子文件夹 - Outlook - Save file from email with .xls attachment and from specific sender then move email to sub folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM