By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,553 Members | 835 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,553 IT Pros & Developers. It's quick & easy.

Email folder import with attachment

P: 9
Hey guys

I'm trying to import Email folder but I can't find a way to import the attachment as well

Knowing that there is a cell that determines whether there is an attachment or not

So what I need is a code to be associated to a button that can show the attachment(s)
2 Weeks Ago #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 1,247
I guess "importing it" implies some preferred destination, which in your case I cannot speak to. But here is how you can save an email attachment. If you want to show it, it will depend on the file type and what tool or program you use to show/open the file.

Here's a slice of code I wrote. I've pulled some selected lines out of a block of code and added the necessary next/ends for this quick reply

Expand|Select|Wrap|Line Numbers
  1.       Dim Folder As Outlook.MAPIFolder
  2.       Dim SubFolder As Outlook.MAPIFolder
  3. 250   For Each Folder In objNSpace.folders
  4. 260       For Each SubFolder In Folder.folders
  5. 270       For Each UserFolder In SubFolder.folders
  6. 280       For Each objItem In UserFolder.Items  '  myFolder.Items
  7. 290           If objItem.Class = olMail Then
  8.                   Dim objMail As Outlook.MailItem
  9. 300               Set objMail = objItem
  10.  
  11. 380               For Each att In objItem.Attachments
  12. 390                   bSkip = False
  13. 400                   If objMail.ReceivedTime <= rsEmail!LastEmailReceived Then
  14. 410                     att.SaveAsFile Path_and_Fname 
  15. 420                   End If
  16.                end if  ' objitem.class=olMail
  17.     next objitem
  18.     next userfolder
  19.     next subfolder
  20.     next folder
  21.  
2 Weeks Ago #2

P: 9
I changed the UserFolder.Items to Inbox.Items however it didn't work

If you can help of how it use the code or where can I assign it to let it work
2 Weeks Ago #3

ADezii
Expert 5K+
P: 8,750
I am also confused as to what you mean by 'Import', but the following Code will:
  1. Create an Instance of Microsoft Outlook.
  2. Access the Default Inbox.
  3. Loop thru every Email in the Inbox.
    1. If the Email contains 1 or more Attachments, Print and sequentially number the Emails to the Immediate Window.
  4. Perform some basic Cleanup Chores.
  5. Obviously, change to suit you specific needs.
  6. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. 'Must set a Reference to the Microsoft Outlook XX.X Object Library
    2. Dim appOut As Outlook.Application
    3. Dim olNS As Outlook.Namespace
    4. Dim oFldr As Outlook.MAPIFolder
    5. Dim oItem As Outlook.MailItem
    6. Dim oAtch As Outlook.Attachment
    7. Dim intNumOAttch As Integer
    8. Dim intCtr As Integer
    9.  
    10. Set appOut = New Outlook.Application
    11. Set olNS = appOut.GetNamespace("MAPI")
    12. Set oFldr = olNS.GetDefaultFolder(olFolderInbox)
    13.  
    14. For Each oItem In oFldr.Items
    15.   If oItem.Attachments.Count > 0 Then
    16.     Debug.Print oItem.Subject
    17.       For intCtr = 1 To oItem.Attachments.Count
    18.         Debug.Print "  |-- " & Format$(intCtr, "00") & ":  " & oItem.Attachments(intCtr)
    19.       Next
    20.     Debug.Print "---------------------------------------------------------------------------------"
    21.   End If
    22. Next oItem
    23.  
    24. Set oFldr = Nothing
    25. Set olNS = Nothing
    26. Set appOut = Nothing
  7. Sample OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. Video Project request process
    2.   |-- 01:  Video Production Request Form.pdf
    3.   |-- 02:  Video Edit Request Form.pdf
    4.   |-- 03:  Production Release Form.pdf
    5. --------------------------------------------------
    6. Phone list
    7.   |-- 01:  My Contacts Phone List 19-20.docx
    8. --------------------------------------------------
    9. Vacation Photos (September 2020) 
    10.   |-- 01:  image077.jpg
    11.   |-- 02:  image013.jpg
    12.   |-- 03:  image014.jpg
    13.   |-- 04:  image015.jpg
    14.   |-- 05:  image016.jpg
    15.   |-- 06:  image017.jpg
    16.   |-- 07:  image018.jpg
    17.   |-- 08:  image019.jpg
    18.   |-- 09:  image020.jpg
    19. --------------------------------------------------
    20. FW: RE: Accessibility issues for people who can't wear masks
    21.   |-- 01:  image002.jpg
    22.   |-- 02:  image003.png
    23.   |-- 03:  image004.png
    24.   |-- 04:  image005.png
    25.   |-- 05:  image006.png
    26.   |-- 06:  image007.png
2 Weeks Ago #4

P: 9
Thanks a lot for the reply
What I'm doing is I'm trying to collect emails using ms access, so at the beginning I've used the import from external data option to get the email folder into a table in access and then I created a form to be able to see the email contents, sender ... etc.
However if there is an attachment in the mail I don't know how to preview it within the access form.

adding to this I don't know to what exactly do I assign the code.
1 Week Ago #5

ADezii
Expert 5K+
P: 8,750
  1. Save each Attachment to Disk using the SaveAsFile() Method of the Attachment Object
  2. Created a Related Table to your EMail Table that stores the Absolute PATHs to the previously saved Attachments. A Foreign Key in this Table will Link to the EMail Table.
  3. You can create a Sub-Form on you Main Form that will contain the Attachment Information
  4. Using any of several Methods, clicking on an Attachment Record in the Sub-Form will open that Attachment.
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.