Consulting

Results 1 to 3 of 3

Thread: Outlook email printing

  1. #1
    VBAX Newbie
    Joined
    Nov 2015
    Posts
    2
    Location

    Question Outlook email printing

    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.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Nov 2015
    Posts
    2
    Location
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •