I'm writing a script that brings emails from an O365 mailbox into MS Access. Accessing the emails, and exporting the attachments all works fine. Records can be created in the DB with sender, date/time, subject, body etc.
However, I am unable to add the attachments into the DB's attachment field.
This line always generated a type mismatch error:
Set rsAttach = rstDocs.Fields("Attachments").Value
rstDocs
already was used to update the other fields in the record - no issue.
But I need the rsAttach
object to load the file attachment.
Already tried numerous variations of creating and defining the rsAttach
object. I Tried the online examples and I'm basically as per the book from MSDN and other examples from this web site.
omEmail
is the email object from Outlook. Its already saved in TmpPath
when we get to here.
Set cn = CreateObject("ADODB.Connection")
Set rstDocs = CreateObject("ADODB.Recordset")
Set rsAttach= CreateObject("ADODB.Recordset")
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\data\CustService.accdb';Persist Security Info=False;"
cn.Open
rstDocs.Open "Documents;", cn, adOpenKeyset, adLockPessimistic, adCmdTable
rstDocs.AddNew
rstDocs("Sender").Value = omEmail.SenderEmailAddress
For Each Attachment In omEmail.Attachments
Set rsAttach = rstDocs.Fields("Attachments").Value ' ERROR HERE
rsAttach.AddNew
rsAttach.Fields("FileData").LoadFromFile TmpPath + Attachment.FileName
rsAttach.Update
Next
rstDocs.Update
rstDocs.Close