Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Printing All Outlook Attachments
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bass_maniac




PostPosted: Fri Aug 29, 2008 7:08 pm   Post subject: 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.

Quote:
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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Aug 29, 2008 7:26 pm   Post subject: RE:Printing All Outlook Attachments

Perhaps you shouldn't be naming your variables the same names as methods.
code:

For Each Item In Inbox.Items
   For Each Atmt In Item.Attachments
      ...
      Item.Atmt.Item(i).PrintOut
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
bass_maniac




PostPosted: Fri Aug 29, 2008 7:55 pm   Post subject: 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




PostPosted: Fri Aug 29, 2008 8:39 pm   Post subject: 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
code:

Item.Atmt.Item(i)

?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
bass_maniac




PostPosted: Thu Sep 04, 2008 4:02 pm   Post subject: 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,

Quote:
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".
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: