Consulting

Results 1 to 3 of 3

Thread: Outlook VBA macro batch printing attachment multiple e-mail

  1. #1

    Outlook VBA macro batch printing attachment multiple e-mail

    Hello,
    About a week ago, I found a macro to batch print attachments, from the emails which I would select.
    It works flawlessly and prints out all of the attachments within the selected emails.
    However I have recognized a problem with the macro afterall, which is that the macro doesn't print the emails in any sort of order.
    Is there by any chance, a way to make the macro print the emails from the top, and work it's way down through the folder?
    At the time of writing, the macro will print in an unwanted order.
    To clarify what I mean, I want it to print out with the most recent email being the 1st, and the 2nd most recent email being the 2nd printed.
    Right now the order is somewhat messed up, and it would probably print in an order like "1, 2, 4, 3, 5, 7, 6, 8", whereas I would like it to print in a chronological order "1, 2, 3, 4, 5, 6, 7, 8".
    Is this possible to adjust the macro, so it will print the attachments in order?
    My macro is as listed below:


    Sub BatchPrintAllAttachmentsinMultipleEmails()
         Dim objFileSystem As Object
         Dim strTempFolder As String
         Dim objSelection As Outlook.Selection
         Dim objItem As Object
         Dim objMail As Outlook.MailItem
         Dim objAttachments As Outlook.Attachments
         Dim objAttachment As Outlook.Attachment
         Dim objShell As Object
         Dim objTempFolder As Object
         Dim objTempFolderItem As Object
         Dim strFilePath As String
      
         Set objFileSystem = CreateObject("Scripting.FileSystemObject")
         strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")
         'Create a new temp folder
         MkDir (strTempFolder)
      
         Set objSelection = Outlook.Application.ActiveExplorer.Selection
      
         For Each objItem In objSelection
             If TypeOf objItem Is MailItem Then
                Set objMail = objItem
                Set objAttachments = objMail.Attachments
      
                'Save all the attachments in the temp folder
                For Each objAttachment In objAttachments
                    strFilePath = strTempFolder & "\" & objAttachment.FileName
                    objAttachment.SaveAsFile (strFilePath)
      
                    'Print all the files in the temp folder
                    Set objShell = CreateObject("Shell.Application")
                    Set objTempFolder = objShell.NameSpace(0)
                    Set objTempFolderItem = objTempFolder.ParseName(strFilePath)
                    objTempFolderItem.InvokeVerbEx ("print")
                Next objAttachment
             End If
         Next
     End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    bump
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Oops - wrong thread.
    Last edited by gmayor; 10-24-2017 at 09:23 PM. Reason: Wroing thread :(
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

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
  •