none
Automated Attachment Printing Outlook 2010

    Question

  • Hi All

    My problem is this,  i'm trying to find a solution for our telesales department in automated email/attachment printing. They recieve orders via email, most of which come with attachments and we need to find a way of printing these automatically as they come in as it is now becoming a fulltime job. I have looked at addins etc but feel a macro might be the way forward, problem is my programming skills are next to zero.

    Can anybody suggest something?


    edit* it might be worth noting that the attachments come in a variety of file types = .pdf, .rtf, .xls, .xlsx, .html, .jpg
    Wednesday, July 20, 2011 12:20 PM

Answers

  • You could use VBA+a rule.

    Add the following code to ThisOutlookSession module.

    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)
          
          'prints 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

    Add a reference to Microsoft Scripting Runtime from Tools\References.

    Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.

     


    Thursday, July 21, 2011 6:38 AM

All replies

  • You could use VBA+a rule.

    Add the following code to ThisOutlookSession module.

    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)
          
          'prints 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

    Add a reference to Microsoft Scripting Runtime from Tools\References.

    Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.

     


    Thursday, July 21, 2011 6:38 AM
  • I will give this a go, thank you!
    Thursday, July 21, 2011 9:12 AM
  • Hi Alexandru,

     

    how do i use that vba printing current page. i dont want to print whole dokument

    Tuesday, October 4, 2011 1:51 PM
  • For a few VBA scripts are nothing new, However, this should be an available add-in from Microsoft. This script has too many uses for businesses to mention. Whether its creating a hard-copy for shipping, order handling or a digital copy in a network folder, automatically printing attachments from trusted sources would be very helpful. Microsoft needs to answer the question "Is Microsoft's software designed to be usefule and/or helpful?"
    Thursday, July 26, 2012 7:45 PM
  • "You could use VBA+a rule.

    Add the following code to ThisOutlookSession module."

    Where to you go to add VBA script?

    Thursday, July 26, 2012 7:48 PM
  • In ThisOutlookSession module.

    Friday, July 27, 2012 5:58 AM
  • Another one of the functions not in the default menu.... arrgg!

    Got the module installed and set for the desired emails. I must have read it wrong at the OP's post. I was hoping this would only print the attachment. The body of the email's I am receiving is a generic cover that I do not need at all.

    Is there a different VBA or mod so that Only the attachment will print?

    Tuesday, August 14, 2012 9:35 PM
  • See http://www.vboffice.net/sample.html?mnu=2&lang=en&smp=3&cmd=showitem - it's intended to print as the message arrives, but will work if you move a message from one folder to another.


    Diane Poremsky [MVP - Outlook]
    Outlook Daily Tips | Outlook & Exchange Solutions Center
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Tuesday, August 14, 2012 11:28 PM
  • Thanks. I have already it tried it, but no success. I have some things to take care that I can't set aside some real time for a week or two to investigate what's going on for me.

    My Environment is W7Pro, Office 2010. I know there have been some underlying changes by MS that may be the issue, and your posting (linked page) is dated from 2006...

    Also, the main document type I am looking to have print are PDF's....

    Wednesday, August 15, 2012 1:58 PM
  • Page age is not a big deal, many macros written for old versions still work with little or no tweaking. That macro has one limitation though - it's for 32bit only (the first couple of lines need updated for 64bit).

    For pdf and other file types, as long as you have an application installed, you just need to (or change) the file types in the Case line.



    Diane Poremsky [MVP - Outlook]
    Outlook Daily Tips | Outlook & Exchange Solutions Center
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Wednesday, August 15, 2012 8:46 PM
  • As an FYI, the code sample at vboffice.net works with Outlook 2013 32-bit, so it should work just fine with Outlook 2010 32-bit.  (For 4 character extensions, you need to change the 4 to a 5 where it checks the filename.)


    Diane Poremsky [MVP - Outlook]
    Outlook Daily Tips | Outlook & Exchange Solutions Center
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Wednesday, August 15, 2012 9:06 PM
  • You could use VBA+a rule.

    Add the following code to ThisOutlookSession module.

    VBA:

    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)
          
          'prints 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


    Add a reference to Microsoft Scripting Runtime from Tools\References.

    Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.

     

    This is a fantastic solution, I followed your instructions and found its worked a dream, well done thank you.

    additional help for setting up VBA Scripting:

    http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/

    Regards,

    Keyth

    Tuesday, October 22, 2013 7:27 AM
  • I'm glad that it worked. :)
    Wednesday, October 23, 2013 1:49 PM
  • I'm glad that it worked. :)
    i know this was answered and this is a amazing script.  i use it myself also now.  but i need it sightly modified if possible.  i print pdf's alot however when they are scanned they are on 8 1/2 by 13 paper and i am only able to print normal 8 1/2 by 11.  so when it prints it is losing 2 inches of the doc.  is there a way that it can shrink to fit? 
    Thursday, October 31, 2013 12:42 AM
  • Did the AcrobatPrint macro not work?


    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Thursday, October 31, 2013 1:20 AM

  • No, i couldn't get it to work, i tried but it wanted to do every email.  i need to be able to use a rule and tell it to use certain subjects and this script is more convient for it. but im still open for ideas

    Thursday, October 31, 2013 11:23 AM
  • You need to merge the two - get the AcrobatePrint sub then tweak the code above to something like this - replace the block between saving the file and moving on to the next attachment.

    ' at top
    Dim sFileType As String

    ' replace code block
    oAtt.SaveAsFile (FullFile)

          sFileType = LCase$(right$(oAtt.FileName, 4))

        Select Case sFileType

    ' Add additional file types below
    ' use shell to print using default app
          Case ".xls", ".doc", "docx"
     
          Set objShell = CreateObject("Shell.Application")
          Set objFolder = objShell.NameSpace(0)
          Set objFolderItem = objFolder.ParseName(FullFile)
          objFolderItem.InvokeVerbEx ("print")

            
    ' Print PDF using AcrobatPrint 
         Case ".pdf"

            AcrobatPrint FullFile, "All"
     
          End Select

    Next oAtt



    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Thursday, October 31, 2013 5:09 PM
  • here is my updated code:

    Sub LSPrint(Item As Outlook.MailItem)
        On Error GoTo OError
         
        'detect Temp
        Dim oFS As FileSystemObject
        Dim sTempFolder As String
        Dim sFileType 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
         
        sFileType = LCase$(Right$(oAtt.FileName, 4))
     
        Select Case sFileType
     
    ' Add additional file types below
     ' use shell to print using default app
           Case ".xls", ".doc", "docx"
         
          'prints attachment
          Set objShell = CreateObject("Shell.Application")
          Set objFolder = objShell.NameSpace(0)
          Set objFolderItem = objFolder.ParseName(fullfile)
          objFolderItem.InvokeVerbEx ("print")

    ' Print PDF using AcrobatPrint
          Case ".pdf"
     
            AcrobatPrint fullfile, "All"
     
           End Select

        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

    when i run it i am getting a compile error:

    ByRef argument type mismatch

    can you please help again im sorry

    Thursday, October 31, 2013 5:57 PM
  • sorry about this,

    the compile error is on this line

     AcrobatPrint fullfile, "All

    and it highlights fullfile

    Thursday, October 31, 2013 5:58 PM
  • oAtt.SaveAsFile (FullFile) is missing

    and the acrobat sub uses filename - your macro uses FullFile - they need to match. Change filename to FullFile - i see it twice in the Acrobat code.  

    and finally, it worked when i added      Dim FullFile As String to the top of the LSPrint macro.



    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Thursday, October 31, 2013 7:25 PM
  • Thank you for this code.. however when I follow the steps I get "Object Required (Error 424)" am I missing something? This is exactly what I need to happen in my office.. our environment is Win 7 Pro, Office 2010 32-bit SP2

    Thank you in advance..

    Wednesday, November 20, 2013 6:20 PM
  • I have tried the code and it works great however in my case I would want to the script to auto print attachments from a Shared Mailbox setup in my outlook. I tried setting up the Shared Mailbox as a second email account and configured the rule to select the 2nd email account but it doesn't work. Any suggestions?
    Tuesday, December 10, 2013 5:42 PM
  • I have the same problem - same environment.

    Thanks

    Thursday, December 19, 2013 6:43 PM
  • One way is to use itemadd and watch for new items in the mail.

    This uses an itemadd macro:
    http://www.slipstick.com/developer/print-attachments-as-they-arrive/

    This shows you how to work with folders in other data stores:
    http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/



    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Thursday, December 19, 2013 10:58 PM
  • Thanks Alexandru. This was exactly what I was looking for in OL2003. No need for any middleware now.

    Take care,

    Chris

    Blue Book Services, Inc.

    Thursday, June 19, 2014 2:05 PM
  • Just take Print Tools for Outlook from MAPILab: http://www.mapilab.com/outlook/print_tools/

    WBR, Aleksandr

    Monday, July 7, 2014 7:53 AM
  • I keep getting the error

    Compile error:

    User-defined type not defined

    What am i doing wrong?? 

    Thursday, September 11, 2014 8:31 AM
  • I'm guessing the error is because of this:

    Dim oFS As FileSystemObject

    You need to set a reference to scripting runtime in Tools, References or use this to replace the dim ofs and set ofs lines in the code

    Dim oFS As Object
    Set oFS = CreateObject("Scripting.FileSystemObject")



    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Thursday, September 11, 2014 3:25 PM
  • Diane,

    Hi, I was hoping you could help give me a tip on where to add the ptrdeclare into the script to make it work with 64 bit outlook? I have been reading up on it but just cant work it out, i am new to VB Scripting (evidently)

    Thanks

    Roy

    Monday, August 3, 2015 10:18 AM
  • replace the word Declare with Declare PtrSafe (at the top of Michael's code sample)


    Diane Poremsky [MVP - Outlook]
    Outlook & Exchange Solutions Center
    Outlook Tips
    Subscribe to Exchange Messaging Outlook weekly newsletter

    Monday, August 3, 2015 6:57 PM
  • Hello Alexandru,

    Thank you for sharing this great piece of code. I am having problems where the code is only printing the message from the e-mail but not the "attachment". I am interested in printing the attachment every time it is sent to me. Can you help?

    Tuesday, August 11, 2015 11:41 AM
  • Have you done this:

    "Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action." ?

    Without that rule you can't print the attachment.

    Tuesday, August 11, 2015 3:22 PM
  • I am using XP, it will launch Adobe and print the PDF attachment but also launches Windows Print program. Any way to prevent this? 
    Saturday, August 22, 2015 6:56 PM
  • Alexandru,

    Your code works great! It automatically prints an attachment. However, it only prints one single attachment. If I receive an email with "X" amount of attachments, the code will print "X" amount of copies of just one of the attachments. How can I modify your code so that it'll print all the attachments? Also, who can I get the code to print two copies of each attachment? I currently just have the "print" portion of the code copied twice and it works, but I wish to know a better way. Thanks!

    Thursday, October 8, 2015 6:33 PM
  • Hi Alexandru,

    have you got or updated VBA code attached to above VBA code, once attachment prints it moves to specific folder?

    Thank you

    Suresh

    Friday, April 8, 2016 9:27 AM
  • I am also trying to have emails with attachments automatically printed.  I have copied and pasted the code above, and created a rule and script, but am receiving an error 75 File Path.  Upon debug, I know it is the MkDir (cTmpFld).  I'm not sure what to do.


    Monday, April 10, 2017 8:23 PM
  • Hi all

    I use this code to automatically print outlook pdf attachment with small changes.

    The code opens and closes the document to be printed, but the adobe reader program remains Open.

    Can someone help me explain how to make the adobe reader program close after printing the document?

    Thanks in advance.

    Pacheco Vieira

    Sunday, March 4, 2018 12:29 PM