Log in

View Full Version : Outlook email printing



gerard87
11-12-2015, 08:51 AM
I need to print 20 copies of an email attachment from a certain sender as they are received. Can someone please help with a vba Marco.

gmayor
11-12-2015, 10:30 PM
This is simple enough to achieve, but it would help to know what the attachment file type is. However the following code in a standard VBA module should do the trick. You can adjust the list of file types according to what you are expecting, and change the path to an existing folder.

The process will print all attachments that meet the criteria in the messages (run from a rule to identify the required messages to process) as they arrive. The macro will overwrite any existing message of the same name, but as you haven't indicated what you want to happen to the saved attachments after printing, I am not going to guess your intentions.
Option Explicit

Private Declare 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

Sub PrintAttachments(Item As Outlook.MailItem)
Dim olAtt As Attachment
Dim strFileName As String
Dim strFileType As String
Const strPath As String = "C:\Path\" ' The folder to save the attachments
If Item.Attachments.Count > 0 Then
For Each olAtt In Item.Attachments
strFileType = LCase$(Right$(olAtt.FileName, 4))
Select Case strFileType
' The file types to print (note 4 characters)
Case "xlsx", "docx", ".pdf", ".doc", ".xls"
strFileName = strPath & olAtt.FileName
olAtt.SaveAsFile strFileName
' Print saved attachements
ShellExecute 0, "print", strFileName, vbNullString, vbNullString, 0
End Select
Next olAtt
End If
lbl_Exit:
Exit Sub
End Sub

gerard87
11-13-2015, 09:21 AM
Hi Gmayor

thanks for your input that is great, I ll explain further what I use it for. I work for a youth club and each morning the activities for each member of staff is emailed through one hour before opening. The reason why we need something like this is we have a very old printer and it takes a long time to print anything off. An email is sent by email address with a PDF attachment I need it opened and not saved and 20 copies printed off.