Outlook VBA Scripting – Print Attachments & Move Emails

0

I am new to VB scripting so need a lot of help.

A recent change to an email account means emails received are been moved to a folder other than the inbox by a rule that I have not input into and cant change, lets call it Folder_X.

What I'm trying to do is auto print attachments from any email that arrive in Folder_X that has an attachment. Once the attachment is printed move the email to another folder (Folder_Y). Any email that doesn't have an attachment should't be moved.

Previously I was able to use a rule on incoming mail, moving it to Folder_Y if it had an attachment and running the following script that I found on the internet to print the attachment. But with this new rule setup that I don't have input into, I cant use the previous rule anymore as rules only work on inbound/outbound mail and not mail already in a folder (Folder_X).

Sub LSPrint(Item As Outlook.MailItem)
    On Error GoTo OError

    'detect Temp
    Dim oFS As FileSystemObject
    Dim sTempFolder As String

    Set oFS = New FileSystemObject
    'Temporary Folder Path
    sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
    'creates a special temp folder
    cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
    MkDir (cTmpFld)

    'save & print
    Dim oAtt As Attachment

    For Each oAtt In Item.Attachments
      FileName = oAtt.FileName
      FullFile = cTmpFld & "\" & FileName

      'save attachment
      oAtt.SaveAsFile (FullFile)

      'print attachment
      Set objShell = CreateObject("Shell.Application")
      Set objFolder = objShell.NameSpace(0)
      Set objFolderItem = objFolder.ParseName(FullFile)
      objFolderItem.InvokeVerbEx ("print")
    Next oAtt

    'Cleanup

    If Not oFS Is Nothing Then Set oFS = Nothing
    If Not objFolder Is Nothing Then Set objFolder = Nothing
    If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
    If Not objShell Is Nothing Then Set objShell = Nothing

OError:

    If Err <> 0 Then
      MsgBox Err.Number & " - " & Err.Description
      Err.Clear
    End If

    Exit Sub
End Sub

Any suggestions on how to adapt this script to work on an folder, or an alternative way of doing this would be greatly appreciated.

N.Fitz

Posted 2018-03-28T12:15:03.913

Reputation: 3

You can run rules manually on any folder with “Run Rules Now”. – niton – 2018-04-03T18:53:59.703

Thanks for the reply. Unfortunately management want this to be totally automated, this email account receives hundreds of emails with invoices attached everyday and they dont trust the person who has access to this account to stay on top of the printing even using a method as simple as you have suggested. – N.Fitz – 2018-04-05T08:12:37.497

Answers

0

You can use the ItemAdd event to run code after an item enters a folder.

Option Explicit

'  In ThisOutlookSession
Private WithEvents addedItems As Items

Private Sub Application_Startup()
    ' Add as many  .folders(subfolder name) as is needed to navigate to the folder
    Set addedItems = Session.GetDefaultFolder(olFolderInbox).folders("folder_X").Items
End Sub

Private Sub addedItems_ItemAdd(ByVal Item As Object)

    Dim oAtt As attachment

    If Item.Attachments.count > 0 Then

        Debug.Print "Processing " & Item.subject

        For Each oAtt In Item.Attachments
            Debug.Print "Processing attachment."
        Next oAtt

        Item.move Session.GetDefaultFolder(olFolderInbox).folders("folder_Y")

    End If

End Sub

niton

Posted 2018-03-28T12:15:03.913

Reputation: 1 724