473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Extract Email Attachment into MS database

17 16bit
I am working on simple task monitoring tool build in MS Access 2016. I have link the outlook inbox into my access db. but I am only getting text, I am looking hit how I could get attachment from the emails into the db as well.

Is it possible to do this through vba? I have wrote this code but its not working as intended. Any help will be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. Private Sub GetAttachments()
  2.  
  3.     Dim ns As Namespace
  4.     Dim Inbox As Outlook.MAPIFolder
  5.     Dim Item As Object
  6.     Dim Atmt As Outlook.Attachment
  7.     Dim FileName As String
  8.  
  9.     Set ns = GetNamespace("MAPI")
  10.  
  11.     Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  12.     Set Inbox = ns.Folders("MailboxName").Folders("Inbox")
  13.  
  14.     If Inbox.Items.Count = 0 Then
  15.         MsgBox "There are no messages in the Inbox.", vbInformation, _
  16.                 "Nothing Found"
  17.         Exit Sub
  18.     End If
  19.  
  20.     For Each Item In Inbox.Items
  21.         For Each Atmt In Item.Attachments
  22.             If Atmt.Type = 1 And InStr(Atmt, "xlsx") > 0 Then
  23.                 FileName = "C:\attachments\" & Atmt.FileName
  24.                 Atmt.SaveAsFile FileName
  25.             End If
  26.         Next Atmt
  27.     Next Item
  28.  
  29. End Sub
  30.  
May 11 '19 #1
11 3989
NeoPa
32,556 Expert Mod 16PB
Adict:
not working as intended
Not sure that's very helpful if I'm honest.

Bear in mind we aren't at your computer and only have what you tell us to go in. So, not much then.

What happens? Where does it break? How does it break?
May 11 '19 #2
Adict
17 16bit
So the code above runs well but its looking at all the emails in the outlook folder and extracting emails, what i need is something like every email received gets registered into my internal table called Inbox with the rest of the text and attachment from that single email. Search much but didn't come across any . appreciate if i can be pointed to right direction
May 12 '19 #3
zmbd
5,501 Expert Mod 4TB
Adict,
1) Rarely a good idea to store files in an access database. The file size of the database is fairly small and the bloat is horrendous.

2) You have no reference to the database in your code.

3) Your follow-up is still a bit obscure, maybe it's the late hour/long day I have had?
Perhaps, try to explain what you want in a step by step fashion?

4) Are you running this code from Outlook or from Access?
May 12 '19 #4
Adict
17 16bit
Thank you, i wish to run the code from my access database.
1. I have link the particular outlook mail box into my database but i only get text which since into my table called email.

2. What i intend to achieve is to extract outlook attachment into each records with a click event from Access Database.

3.Extract the specific email into a shared network folder and have the link inserted into my table.

Thanks in advance
May 12 '19 #5
NeoPa
32,556 Expert Mod 16PB
NeoPa:
What happens? Where does it break? How does it break?
Sorry. Let's try again.

I'm happy you're described what you want from the code. I'm also happy you've started the code already and posted what you have. All good. I'm less happy that you haven't explained what's actually happening when you try to run it.

I hope it's now very clear what it is I'm asking for.

PS. I agree with Zmbd about the advisability of storing files in a database. Even in bigger ones (than Access) it's a something that would need careful consideration. Nevertheless, it never hurts to learn how to do things. Your judgement as to what you use when you know how is up to you. We're here to help you to know how.
May 12 '19 #6
zmbd
5,501 Expert Mod 4TB
1. I have link the particular outlook mail box into my database but i only get text which since into my table called email.
Did you create this link by:
Ribbon>External Data>Import & Link/More>Outlook Folder
(x)Link to the data source....

The problem with this method is that there is typically no message_id field which will make it difficult to associate any attachments with the corresponding email.
May 12 '19 #7
ADezii
8,834 Expert 8TB
If you wish to avoid and Link and save the Attached Files to a Folder on your Hard Drive, the following Code will work well (assuming you have a Folder named 'C:\Attachments'). You could then simply store the PATH to these Files within your DB.
Expand|Select|Wrap|Line Numbers
  1. Dim ns As Outlook.Namespace
  2. Dim appOutlook As Outlook.Application
  3. Dim Inbox As Outlook.MAPIFolder
  4. Dim Item As Outlook.MailItem
  5. Dim Atmt As Outlook.Attachment
  6. Dim FileName As String
  7.  
  8. Set appOutlook = New Outlook.Application
  9. Set ns = appOutlook.GetNamespace("MAPI")
  10. Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  11.  
  12. If Inbox.Items.Count = 0 Then
  13.   MsgBox "There are no messages in the Inbox.", vbInformation, _
  14.          "Nothing Found"
  15.     Exit Sub
  16. End If
  17.  
  18. For Each Item In Inbox.Items
  19.   For Each Atmt In Item.Attachments
  20.     If Atmt.Type = 1 And InStr(Atmt, "xlsx") > 0 Then
  21.       FileName = "C:\attachments\" & Atmt.FileName
  22.         Atmt.SaveAsFile FileName
  23.     End If
  24.   Next
  25. Next Item
May 12 '19 #8
zmbd
5,501 Expert Mod 4TB
ADezii,
Essentially where I was going with this...

My thought was:
Table with the email text information
Table with a [FK] to the email table with would store the path information for the saved attachments to handle emails with multiple attachments.
May 13 '19 #9
Adict
17 16bit
Thanks Zmdb, I did try that by inserting a code on the outlook to run a insert function when a new mail arrived into the Inbox. However it didn't run as intended. Here the code i wrote in outlook that I ended up getting error with
Expand|Select|Wrap|Line Numbers
  1. Dim HRSWorkflow As MailItem
  2. Set HRSWorkflow = Application.Session.GetRecipientFromID(EntryIDCollection)
  3.  
  4. Dim WkfSubject As String, WkfSender As String, wrfSenderEmail As String, wkfrequestdate As Date, wkfdescription As String, wkfcc As String
  5.  
  6.  
  7. WkfSender = HRSWorkflow.Sender
  8. wrfSenderEmail = HRSWorkflow.SenderEmailAddress
  9. WkfSubject = HRSWorkflow.Subject
  10. wkfcc = HRSWorkflow.CC
  11. wkfrequestdate = HRSWorkflow.ReceivedTime
  12. wkfdescription = HRSWorkflow.Body
  13. '_______________________________________________________________
  14. Dim str As String
  15.  
  16. str = "INSERT INTO TABLE tblTaskCollector(Sender,SenderEmail,Subject, CC,Description,DateRecieved) VALUES(" & "'" & WkfSender & "'" & "," & "'" & wrfSenderEmail & "'" & "," & "'" & WkfSubjec & "'" & "," & "'" & wkfcc & "'" & "," & "'" & wkfrequestdate & "'" & "," & "'" & wkfdescription & "'" & ", )"
  17. Dim cnx As ADODB.Connection
  18. Set cnx = New ADODB.Connection
  19.  
  20. cnx.Provider = "Microsoft.ACE.OLEDB.12.0"
  21. cnx.ConnectionString = "C:\Users\PosiaE\Documents\HR Transacts.accdb"
  22. cnx.Open
  23. cnx.Execute (str)
May 13 '19 #10
Adict
17 16bit
How can i get a unique id to identify this attachment belong to this email? Apology, I ma having my head cracked by this. Really appreciate all your feedback.
May 13 '19 #11
ADezii
8,834 Expert 8TB
  1. In line with zmbd's Post #9, I am making some assumptions here strictly to illustrate a point. These assumptions are:
  2. You have a Table named tblAttachments with two Fields ([FK]-{TEXT}, [FPath]-{TEXT}). [FK] is a Foreign Key Field and relates to a Primary Key Field in a MASTER Table.
  3. The Foreign Key Value can somehow be derived from the E-Mail. In this case, I simply used the first six characters of the Subject Line as the Foreign Key.
  4. The above being said, the following Code will:
    1. Process every E-Mail in the Inbox Folder.
    2. If the first 6 characters of the Subject Line is Numeric ([FK]) AND it was Sent after yesterday (5/12/2019) then we loop thru all Attachments in that E-Mail with a *.xlsx Extension.
    3. The Foreign Key is written to the [FK] Field in tblAttachments and the Absolute Path (Filename] is written to the [FPath] Field in tblAttachments.
    4. The actual Attachment(s) is/are saved to the Filename Path.
  5. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim ns As Outlook.Namespace
    2. Dim appOutlook As Outlook.Application
    3. Dim Inbox As Outlook.MAPIFolder
    4. Dim Item As Outlook.MailItem
    5. Dim Atmt As Outlook.Attachment
    6. Dim FileName As String
    7.  
    8. Set appOutlook = New Outlook.Application
    9. Set ns = appOutlook.GetNamespace("MAPI")
    10. Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    11.  
    12. DoCmd.Hourglass True
    13.  
    14. If Inbox.Items.Count = 0 Then
    15.   MsgBox "There are no messages in the Inbox.", vbInformation, _
    16.          "Nothing Found"
    17.     Exit Sub
    18. End If
    19.  
    20. For Each Item In Inbox.Items
    21.   If IsNumeric(Left$(Item.Subject, "6")) And Item.SentOn > #5/12/2019# Then
    22.     For Each Atmt In Item.Attachments
    23.       If Atmt.Type = 1 And InStr(Atmt, ".xls") > 0 Then
    24.         FileName = "C:\attachments\" & Atmt.FileName
    25.           CurrentDb.Execute "INSERT INTO tblAttachments ([FK], [FPath]) VALUES ('" & _
    26.                              Left$(Item.Subject, "6") & "','" & FileName & "')", _
    27.                              dbFailOnError
    28.             Atmt.SaveAsFile FileName
    29.        End If
    30.     Next
    31.    End If
    32. Next Item
    33.  
  6. The Code has been tested and is fully operational under certain conditions. The only problem that I see is that this should all be wrapped in some form of Transaction since you would not want tblAttachments being updated and the SaveAsFile operation fail.
P.S. - If you wish to execute similar Code from Outlook when the E-Mails first arrive, then we are talking about a totally different situation.
May 13 '19 #12

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

Similar topics

3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
3
by: matt roberts | last post by:
Hi... I'm interested in using PHP to extract attachments from a standard unix ..mbox file...anybody done this or have a link to resources that might help? There is plenty on sending mail, but...
4
by: Davor Cengija | last post by:
I need to write a script which should extract the attachment from a text file, which is saved as MIME mail message. Unfortunatelly, Message.is_multipart() returns False so msg.get_payload() returns...
2
by: MLH | last post by:
I routinely save failure notices from mail servers bouncing mail back to me that I sent with invalid address. I would like to write an access procedure in my contacts database that would open the...
7
by: Susan Bricker | last post by:
I would like to generate a report (I have the report working already) using MS/ACCESS 2000 and then have the ability to send the report as an email attachment to my colleagues. I have looked...
0
by: visu | last post by:
I ve a text paragraph something like sample sample sample sample rr@yahoo.com <ee@ss.comsample sample sample sample sample sample sample sample sample sample ds@example.com sample sample sample...
4
by: Alexander Vasilevsky | last post by:
How to extract email address from the letter in Outlook Express? http://www.alvas.net - Audio tools for C# and VB.Net developers
2
by: Alexander Vasilevsky | last post by:
I need extract email address from the address book in Outlook Express also. http://www.alvas.net - Audio tools for C# and VB.Net developers
0
by: peterschedler | last post by:
How do I send an MS Word (2002) document file ( 140 MB )as a .txt email attachment and how can the recipient open it ? I am using ORANGE webmail and Mozilla Firefox .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.