
-----------------------------------
bass_maniac
Fri Aug 29, 2008 7:08 pm

Printing All Outlook Attachments
-----------------------------------
Hi, I'm trying to create a macro in Outlook 2007 to print all of the attachments in an email. I would rather not have to save them to the hard drive and print them from different programs the way I've seen some code examples do it on the web. It seems you should be able to do this all from within Outlook. Here's what I have so far, pieced together from different code examples I've found.

Sub GetAttachments()
    On Error GoTo GetAttachments_err
    
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim Filename As String
    Dim emlApp As Object
    Dim emlDoc As Object
    Dim i As Integer
    
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("Promissory Emails")
    i = 0
    
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the inbox.", vbInformation, "Nothing Found"
        Exit Sub
    End If
    
    For Each Item In Inbox.Items
        For Each Atmt In Item.Attachments
            'The following two lines don't work. This is what I need help with.
            Set emlDoc = Atmt
            Item.Atmt.Item(i).PrintOut
            i = i + 1
        Next Atmt
    Next Item
    
    If i > 0 Then
        MsgBox "I have found " & i & " attached files." & vbCrLf _
        & "I have printed them." _
        & vbCrLf & vbCrLf & "Have a nice day!", vbInformation, "Finished!"
    Else
        MsgBox "I didn't find any attachments in your mail.", vbInformation, "Finished!"
    End If
    
GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
    
GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information:" _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
        
End Sub


Any help would be appreciated as I don't really know VB at all.

-----------------------------------
Tony
Fri Aug 29, 2008 7:26 pm

RE:Printing All Outlook Attachments
-----------------------------------
Perhaps you shouldn't be naming your variables the same names as methods.

For Each Item In Inbox.Items
   For Each Atmt In Item.Attachments
      ...
      Item.Atmt.Item(i).PrintOut 


-----------------------------------
bass_maniac
Fri Aug 29, 2008 7:55 pm

Re: Printing All Outlook Attachments
-----------------------------------
Good point. I have changed the variable Item to mailItem and it still doesn't work. Specifically, I get error 438: Object doesn't support this property or method.

Any ideas?

-----------------------------------
Tony
Fri Aug 29, 2008 8:39 pm

RE:Printing All Outlook Attachments
-----------------------------------
Then you should check the documentation for what methods the object you are working with actually has.

Though really, if you already have your attachment in Atmt, what are you trying to access with

Item.Atmt.Item(i)

?

-----------------------------------
bass_maniac
Thu Sep 04, 2008 4:02 pm

Re: Printing All Outlook Attachments
-----------------------------------
Yeah, I knew that code was wrong, I just didn't know what was supposed to go there. I ended up figuring it out. The code is,

For Each mailItem In Inbox.Items
        mailItem.PrintOut
        For Each Atmt In mailItem.Attachments
            i = i + 1
        Next Atmt
    Next mailItem

Then go to File-->Print... and check off "Print attached files".
