Outlook自动打印多个附件

时间:2018-04-03 15:13:27

标签: vba outlook-vba

由于某种原因,这只是在电子邮件中打印第一个附件。它似乎不像我的for循环工作。任何线索?基本上它会保存附件的备份,打印电子邮件,打印.pdf附件,然后将其分类为"打印"。我需要它在电子邮件上为每个.pdf做这件事。不只是附加的第一个。

 Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Option Compare Text
Sub PrintAttachments(oMail As Outlook.MailItem)
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
Dim strSubject As String

strSubject = oMail.Subject
sDirectory = "MYDIRISHERE"
Set colAtts = oMail.Attachments
If colAtts.Count Then
    For Each oAtt In colAtts
        sFileType = LCase$(Right$(oAtt.FileName, 4))
        Select Case sFileType
        ' Add additional file types below followed by comma
        Case ".pdf"
            If oMail.Categories <> "Printed" Then
                sFile = sDirectory & oAtt.FileName & " " & strSubject & sFileType
                oAtt.SaveAsFile sFile
                oMail.PrintOut
                oMail.Categories = "Printed"
                oMail.Save
                ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
                Debug.Print "Email " & strSubject & " with attachment " & oAtt.FileName & " from " & oMail.SenderName & " Printed."
             End If
        Case Else
            Debug.Print "Attachment: " & oAtt.FileName & " from " & oMail.SenderName & " is not authorized to print."
        End Select
    Next oAtt
End If
End Sub

1 个答案:

答案 0 :(得分:0)

在开始时检查类别,以便#34;不打印&#34;在处理完所有附件之前一直有效。

Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Option Compare Text

Sub PrintAttachments(oMail As MailItem)

Dim colAtts As Attachments
Dim oAtt As Attachment

Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
Dim strSubject As String

strSubject = oMail.Subject
sDirectory = "MYDIRISHERE"

If oMail.Categories <> "Printed" Then

    Set colAtts = oMail.Attachments

    If colAtts.Count Then
        For Each oAtt In colAtts
            sFileType = LCase$(Right$(oAtt.FileName, 4))
            Select Case sFileType

            ' Add additional file types below followed by comma
            Case ".pdf"

                ' This stops the second and subsequent attachments
                '  from being printed since 
                '  the category is prematurely set to "Printed".
                ' If oMail.Categories <> "Printed" Then

                    sFile = sDirectory & oAtt.FileName & " " & strSubject & sFileType
                    oAtt.SaveAsFile sFile
                    oMail.PrintOut
                    oMail.Categories = "Printed"
                    oMail.Save
                    ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
                    Debug.Print "Email " & strSubject & " with attachment " & oAtt.FileName & " from " & oMail.SenderName & " Printed."

                 ' End If

            Case Else
                Debug.Print "Attachment: " & oAtt.FileName & " from " & oMail.SenderName & " is not authorized to print."

            End Select
        Next oAtt
    End If

End If

End Sub