Hi,
With the following code assembled from multiple sources online, I am attempting to download a specific folder in my Outlook Inbox (outlook 365) into my access table called "Email" (Access 2016). I get a Run time error '424' Object required on the line:
If Items.UnRead Then
What am I doing wrong? :banghead: Can someone help me please?
Option Compare Database
Private Sub cmdmail_Click()
Dim adoConn As ADODB.Connection, adoRS As ADODB.Recordset
Dim DBFullName As String
Dim ns As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim i As Long
Set ns = GetNamespace("MAPI")
Set objFolder = ns.PickFolder
'~~> Replace with your database
DBFullName = "C:\mydb.accdb"
Set adoConn = New ADODB.Connection
'~~> Open the database using connection string
adoConn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
Set adoRS = New ADODB.Recordset
If Items.UnRead Then
With adoRS
'~~> Replace Table1 with the name of the table in your database
.Open "SELECT * FROM EMAIL", adoConn, , adLockOptimistic, adCmdText
For i = objFolder.Items.Count To 1 Step -1
With objFolder.Items(i)
If .Class = olMail Then
adoRS.AddNew
adoRS("Subject") = .Subject
adoRS("Body") = .Body
adoRS("FromName") = .SenderName
adoRS("ToName") = .To
adoRS.Update
End If
End With
Next
End With
End If
'~~> Close and Cleanup
adoRS.Close
Set adoRS = Nothing
Set adoConn = Nothing
Set ns = Nothing
Set objFolder = Nothing
MsgBox ("done")
End Sub
With the following code assembled from multiple sources online, I am attempting to download a specific folder in my Outlook Inbox (outlook 365) into my access table called "Email" (Access 2016). I get a Run time error '424' Object required on the line:
If Items.UnRead Then
What am I doing wrong? :banghead: Can someone help me please?
Option Compare Database
Private Sub cmdmail_Click()
Dim adoConn As ADODB.Connection, adoRS As ADODB.Recordset
Dim DBFullName As String
Dim ns As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim i As Long
Set ns = GetNamespace("MAPI")
Set objFolder = ns.PickFolder
'~~> Replace with your database
DBFullName = "C:\mydb.accdb"
Set adoConn = New ADODB.Connection
'~~> Open the database using connection string
adoConn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
Set adoRS = New ADODB.Recordset
If Items.UnRead Then
With adoRS
'~~> Replace Table1 with the name of the table in your database
.Open "SELECT * FROM EMAIL", adoConn, , adLockOptimistic, adCmdText
For i = objFolder.Items.Count To 1 Step -1
With objFolder.Items(i)
If .Class = olMail Then
adoRS.AddNew
adoRS("Subject") = .Subject
adoRS("Body") = .Body
adoRS("FromName") = .SenderName
adoRS("ToName") = .To
adoRS.Update
End If
End With
Next
End With
End If
'~~> Close and Cleanup
adoRS.Close
Set adoRS = Nothing
Set adoConn = Nothing
Set ns = Nothing
Set objFolder = Nothing
MsgBox ("done")
End Sub