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

时间:2011-10-25 14:11:02

标签: vba outlook

我的代码可以将邮件中的附件保存在特定的Outlook文件夹中。

如果电子邮件包含附件,我的脚本将有效,但如果电子邮件是作为附件附件发送的,则无法使用。

在这种情况下,我的电子邮件包含其他电子邮件作为附件(来自自动转发规则)。嵌入的电子邮件附件随后包含excel文件。

请参阅下面我当前的

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

以下代码使用此方法将电子邮件作为附件处理

  1. 测试附件是否是电子邮件(如果文件名以msg结尾)
  2. 如果附件是邮件,则会将其另存为"C:\temp\KillMe.msg"
  3. CreateItemFromTemplate用于将保存的文件作为新消息(msg2)
  4. 进行访问
  5. 然后,代码会处理此临时消息,以将attachmnets剥离到fsSaveFolder
  6. 如果附件不是消息,则根据您当前的代码提取它
  7. 请注意,因为我没有你的olFolder结构,Windoes版本,Outlook变量等我必须添加我自己的文件路径和Outlook文件夹进行测试。您需要更改这些

       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