Option Explicit
Public db As DAO.Database 'Must have Microsoft DAO 3.6 Object Library referenced
Public WithEvents outItems As Outlook.Items
Public Sub Application_Startup()
Dim strDataPath As String
strDataPath = "ZZZ" 'replace this with the path to your database
Set db = CreateObject("DAO.DBEngine.36").Workspaces(0).OpenDatabase(strDataPath)
Set outItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub outItems_ItemAdd(ByVal outItem As Object)
Dim rst As DAO.Recordset
Set rst = db.OpenRecordset(tblname, dbOpenDynaset) 'replace tblname with the name of your table enclosed in quotes
If TypeName(outItem) = "MailItem" Then
With rst
.AddNew
![OrderNo] = outItem.Subject 'manipulate the Subject string with whatever Mid or Instr manipulations you need to in order to extract the oreder number
![DateReceived] = outItem.RecievedTime
.Update
End With
End If
Set rst = Nothing
End Sub
Public Sub Application_Quit()
'free up memory
Set outItems = Nothing
db.Close
Set db = Nothing
End Sub