如何自动打印电子邮件中的附件?

时间:2019-06-07 16:24:17

标签: vba outlook outlook-vba

每天我都会收到数百封电子邮件,其中包含需要打印的发票的pdf附件。

当前,我手动打印它们,这一天最多要花几个小时。

如何使用Outlook-vba在电子邮件中自动打印附件,然后删除该电子邮件。

1 个答案:

答案 0 :(得分:0)

将Microsoft脚本运行时添加到引用中...

创建新规则,然后在我收到的邮件(带有附件/运行脚本)上单击“应用规则”

Option Explicit
Public Sub Example(Item As Outlook.MailItem)
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject

    'Temporary Folder
    Dim TempFldr As String
        TempFldr = Environ("USERPROFILE") & "\Documents\Temp\"
        CreateDir TempFldr

    Dim Atmt As Attachment
    Dim AtmtName As String
    Dim oShell As Object
    Dim Fldr As Object
    Dim FldrItem As Object

    For Each Atmt In Item.Attachments
        AtmtName = TempFldr & Atmt.FileName
        Atmt.SaveAsFile AtmtName

        Set oShell = CreateObject("Shell.Application")
        Set Fldr = oShell.NameSpace(0)
        Set FldrItem = Fldr.ParseName(AtmtName)
            FldrItem.InvokeVerbEx ("print")
    Next Atmt

    'Cleans up
    If Not FSO Is Nothing Then Set FSO = Nothing
    If Not Fldr Is Nothing Then Set Fldr = Nothing
    If Not FldrItem Is Nothing Then Set FldrItem = Nothing
    If Not oShell Is Nothing Then Set oShell = Nothing

End Sub

Private Function CreateDir(FldrPath As String)
    Dim Elm As Variant
    Dim CheckPath As String

    CheckPath = ""
    For Each Elm In Split(FldrPath, "\")
        CheckPath = CheckPath & Elm & "\"

        If Len(Dir(CheckPath, vbDirectory)) = 0 Then
            MkDir CheckPath
            Debug.Print CheckPath & " Folder Created"
        End If

        Debug.Print CheckPath & " Folder Exist"
    Next
End Function