View Full Version : Solved: Attachments in Attachments
jwise
11-07-2007, 04:09 PM
I wrote a macro with a lot of assistance from VBAX to handle e-mail attachments. This was working great. Now I have a problem if I receive an e-mail that has attached e-mails AND these e-mails have the attachments I'm looking for.
The code seems to work IF the e-mail itself has attachments, but not if the e-mail has attached e-mails with attachments. Does that make sense? Here is the problem code:
For Each Atmt In Inbox.Items(j).Attachments
If Right(Atmt.FileName, 3) = frmSearch.txtExt.Value Then
lenFN = Len(Atmt.FileName)
lenFN = lenFN - 4
FN = Left(Atmt.FileName, lenFN)
NewFileName = frmSearch.txtDir.Value & "\" & FN & _
Format(seqNum, "_000#") & "." & frmSearch.txtExt.Value
Atmt.SaveAsFile NewFileName
seqNum = seqNum + 1
i = i + 1
End If
Next Atmt
The code is set-up to handle any particular file-type, but it is almost always attached PDF files that I desire. This is because I receive a variable number of these e-mails, with a variable number of attachments, and there can be duplicate filenames. The "Format" is modifying the filename with a sequence number to handle this, and I am placing the attachments into a user-specified folder.
Any ideas? TIA
Charlize
11-07-2007, 05:20 PM
Check if attachment is emailtype attachment (.eml, ???) . Since this is also a message, you can loop through the attachments of this message to.
jwise
11-08-2007, 08:25 AM
I am having trouble implementing this suggestion. Here is some updated code. The irrelevant code has been dropped:
For Each Atmt In Inbox.Items(j).Attachments
MsgBox Atmt.Type
If Atmt.Type = olEmbeddeditem Then
For Each EmbdAtt In Atmt
MsgBox (EmbdAtt.Type)
Next EmbdAtt
End If
Next Atmt
All this code is doing is the "MsgBox" function, but when I run it, it correctly determines that the attachment is an e-mail with attachments. The second "For Each" loop does not work because of "Error 438- Object does not support this method".
So this question really becomes: Given an attached e-mail, how do I access the attached files in this attachment?
The next questions are: Does Outlook support attachments 'n' levels deep, or is there a limit? Does VBA support recursive descent programming? I am betting I will get three levels deep if I can handle two levels. I've seen everything else in terms of data problems!
FYI: Both Atmt and EmbdAtt are "Dim" as Attachment.
Charlize
11-09-2007, 03:26 AM
I'm afraid it's going to be a hard one. I'm able to filter a .msg attachment and save it but I can not get a handle to this file to treat it as a mailmessage. Once I can succeed in this, I can loop through this file and process the attachments.
jwise
11-09-2007, 07:51 AM
Thanks for your assistance.
According to Sue Mosher "You cannot access the contents of an attachment without saving it to the file system first. That includes embedded Outlook items. You will need to save the item to the file system using the Attachment.SaveAsFile method. You can then use the Application.CreateItemFromTemplate method to return a copy of the saved item and then use the Attachments collection to get the item's attached files."
I started playing with Outlook and I do not believe Outlook does this internally. The size of some of these attachments (and the number of them) tells me that Outlook does not copy the attachments and then process them. I do not mean that I don't believe what Ms. Mosher says- I certainly believe the information. I think that a good solution would be a "recast" something like:
Set myMailItem = Inbox.Items(i).Attachments
This would require that the attachment was indeed an e-mail and not a file, so other code would be needed. Probably this idea of "recast" is totally off base, but is there some way to write a "Method" or "class module" to do the equivalent?
Please note that Ms. Mosher's comment was made in the context that the attachment was indeeed an e-mail and not a file.
The other thing I find so disturbing about this problem is that the sender can make the file attachments at any level. My problem is "attachment of e-mails with file attachments", but this could be extended to further levels. I also have a difficult time believing that others have not encountered this problem. I think I found six hits (all irrelevant) via Google. It seems to me that the best solution would be a routine that would take the item and "flatten" all the attachments. Each attached item has a "type" that indicates whether it is an e-mail or file (I assume there are other possibilities as well), but with this flat model, you would only need to test if it were a file. Then you could process as desired.
Charlize
11-09-2007, 08:05 AM
As a first try out ...Sub Save_Att_From_Att_that_is_mail()
'no of item in loop
Dim vItem
'a message
Dim vMsg As Outlook.MailItem
'attachment of message
Dim vMsgAtt
'attachment of submessage
Dim vSubMsgAtt
'saved attachment as message
Dim myfile As String
'process the mails we have selected
With ActiveExplorer.Selection
For vItem = 1 To .Count
Set vMsg = .item(vItem)
If vMsg.Attachments.Count > 0 Then
MsgBox "Message has " & _
vMsg.Attachments.Count & " attachments", vbInformation
For Each vMsgAtt In vMsg.Attachments
MsgBox "File extension : " & Right$(vMsgAtt.FileName, 3)
If Right$(vMsgAtt.FileName, 3) = "msg" Then
vMsgAtt.SaveAsFile "C:\Data\Attachment_Mail\" & vMsgAtt.FileName
End If
Next vMsgAtt
End If
Next vItem
End With
myfile = Dir("C:\Data\Attachment_Mail\*.*")
Do While myfile <> ""
Set vMsgAtt = Application.CreateItemFromTemplate("C:\Data\Attachment_Mail\" & myfile)
For Each vSubMsgAtt In vMsgAtt.Attachments
MsgBox "Files : " & vbCrLf & _
Left$(vSubMsgAtt.FileName, 255), , "Attachment from Attachment"
Next vSubMsgAtt
Set vMsgAtt = Nothing
myfile = Dir
Loop
End Sub
jwise
11-09-2007, 08:35 AM
It will take a while for me to check this out. The code snippet was stripped from a routine that scanned the Inbox (backwards) for a user- specified number of items. I think the "explorer" stuff means that I am actively looking at the e-mail in question, so I'll need to make some mods.
Thanks again for your gracious assistance.
Charlize
11-09-2007, 08:46 AM
Just go to the mailfolder you want to process and select a bunch of emails (don't open them) with such an attachment (no to many because I used some msgboxes to see what was going on) and then perform that macro.
jwise
11-09-2007, 09:05 AM
Charlize,
I tested the code and it works! Thanks.
I was not accustomed to "selecting" the e-mails, but I followed your instructions and it worked great. I may just use your routine because it will now do what I wanted, or I may try to adapt your code into my routine. My routine takes advantage of the fact that I am only looking for attached PDF files. On the other hand, your routine is more "general purpose". I also only look for e-mails from a particular user (which I can specify through a UserForm). Your code has some advantages over mine in terms of flexibility.
Am I correct in understanding that your code will NOT handle "attachments of attachments of attachments" (three levels deep) . What it does handle is two levels.
Charlize
11-10-2007, 12:51 PM
Am I correct in understanding that your code will NOT handle "attachments of attachments of attachments" (three levels deep) . What it does handle is two levels.H?las, you are correctly assuming this. Only the first message as attachment item will be saved and those attachments from that saved attachment will be handled.
jwise
11-12-2007, 08:17 AM
Thank you for all your suggestions. I decided to use your routine and to put mods in my routine based on your code. So now I have two ways of handling this problem- and other similar problems.
I'm thinking about how I could code this routine to handle any number of levels of attachments. I think the ideal solution would be a sub that returned all attachments and attached e-mails etc as a single level to the original item in the Inbox. Thus you could search this collection of attachments and get all of the pertinent files.
Thanks again for the assistance. I feel like I actually learned something.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.