VBA代码可自动将另一封电子邮件中的Outlook电子邮件中的附件作为附件自动打印

时间:2019-12-13 15:59:23

标签: vba printing outlook email-attachments

有时我们会收到Outlook电子邮件,另一封电子邮件为附件。附件电子邮件本身具有附件,通常为PDF文件。我想使用VBA自动打印附加到第二封电子邮件的附件。我对VBA的经验不是很丰富,但是我想做的似乎是VBA代码的一种变体,shareedit和brettdj于2011年发布了有关VBA代码的信息,用于将Outlook电子邮件中的附件保存在另一封电子邮件中作为附件

以下是在2011年发布到stackoverflow上以保存附件的代码:

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

0 个答案:

没有答案