I am trying to import e-mail from Outlook 2007 into an Access 2007 table. I can import the entire e-mail except for the attachments. I have the following code but get an "Invalid Argument" on the rstAttachment.Fields("FileData") = moAttachment line. For background info, I do not want to save the attachments to the hard drive or a network location, I want them stored in the attachment field in Access.
Sub Load_Mail()
Dim rstAUtemp As DAO.Recordset2
Dim rstAttachment As DAO.Recordset2
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim MailObject As Object
Dim moAttachment As Outlook.Attachment
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").Folders("Mailbox - Address Updates").Folders("Inbox")
Set rstAUtemp = CurrentDb.OpenRecordset("tbl_AU_Staging")
rstAUtemp.AddNew
Set InboxItems = Inbox.Items
For Each MailObject In InboxItems
If MailObject.UnRead Then
With rstAUtemp
.AddNew
!BCC = MailObject.BCC
!Body = MailObject.Body
!CC = MailObject.CC
!ConversationTopic = MailObject.ConversationTopic
!CreationTime = MailObject.CreationTime
!EntryID = MailObject.EntryID
!FlagDueBy = MailObject.FlagDueBy
!FlagRequest = MailObject.FlagRequest
!FlagStatus = MailObject.FlagStatus
!Importance = MailObject.Importance
!ReceivedTime = MailObject.ReceivedTime
!ReminderTime = MailObject.ReminderTime
!SenderName = MailObject.SenderName
!Sensitivity = MailObject.Sensitivity
!SentOn = MailObject.SentOn
!Size = MailObject.Size
!Subject = MailObject.Subject
!To = MailObject.To
!HasAttachment = MailObject.Attachments.Count
If MailObject.Attachments.Count > 0 Then
Set rstAttachment = rstAUtemp.Fields("Attachments").Value
rstAttachment.AddNew
For Each moAttachment In MailObject.Attachments
rstAttachment.Fields("FileData") = moAttachment
Next moAttachment
rstAttachment.Update
End If
!UnRead = MailObject.UnRead
!Load_dte = Now()
.Update
End With
End If
Next
End Sub
Sub Load_Mail()
Dim rstAUtemp As DAO.Recordset2
Dim rstAttachment As DAO.Recordset2
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim MailObject As Object
Dim moAttachment As Outlook.Attachment
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").Folders("Mailbox - Address Updates").Folders("Inbox")
Set rstAUtemp = CurrentDb.OpenRecordset("tbl_AU_Staging")
rstAUtemp.AddNew
Set InboxItems = Inbox.Items
For Each MailObject In InboxItems
If MailObject.UnRead Then
With rstAUtemp
.AddNew
!BCC = MailObject.BCC
!Body = MailObject.Body
!CC = MailObject.CC
!ConversationTopic = MailObject.ConversationTopic
!CreationTime = MailObject.CreationTime
!EntryID = MailObject.EntryID
!FlagDueBy = MailObject.FlagDueBy
!FlagRequest = MailObject.FlagRequest
!FlagStatus = MailObject.FlagStatus
!Importance = MailObject.Importance
!ReceivedTime = MailObject.ReceivedTime
!ReminderTime = MailObject.ReminderTime
!SenderName = MailObject.SenderName
!Sensitivity = MailObject.Sensitivity
!SentOn = MailObject.SentOn
!Size = MailObject.Size
!Subject = MailObject.Subject
!To = MailObject.To
!HasAttachment = MailObject.Attachments.Count
If MailObject.Attachments.Count > 0 Then
Set rstAttachment = rstAUtemp.Fields("Attachments").Value
rstAttachment.AddNew
For Each moAttachment In MailObject.Attachments
rstAttachment.Fields("FileData") = moAttachment
Next moAttachment
rstAttachment.Update
End If
!UnRead = MailObject.UnRead
!Load_dte = Now()
.Update
End With
End If
Next
End Sub