T
tbrown7777
I have a database that i'm essentially moving my inbox into an access
database. I can email information into a table, but I need to be able to
categorize them. I have combo boxes on a form that I would like to use as I
bring over each email so I can catalog them. I either need to figure out how
to show this form during my loop or set the form values to the values i bring
over and wait for me to hit "next" to import the next email. I thought that
I could import the data into the table and then show the userform (the
for/next loop waits until i finish adding the categories) then continues
on.... Here is the code I'm trying to get working.
1. Is it better to add a record and fill the form with the data i bring
over or try to add it to the table first, then show the form?
2. I'm not sure of the syntax for the do.cmd showform so i can edit data.
Private Sub CmdGetEmails_Click()
On Error GoTo Err_CmdGetEmails_Click
Dim Olapp As Outlook.Application
Dim Olmapi As Outlook.NameSpace
Dim Olfolder As Outlook.MAPIFolder
Dim OlDelete As Outlook.MAPIFolder
Dim OlMail As Object 'Have to late bind as appointments e.t.c screw it up
Dim OlItems As Outlook.Items
Dim Rst As DAO.Recordset
Dim db As DAO.Database
Dim InboxCounter As Integer
Set db = CurrentDb
Set Rst = db.OpenRecordset("tblEmail", dbOpenDynaset) 'Open table Email
DoCmd.GoToRecord , , acLast
'Create a connection to outlook
Set Olapp = CreateObject("Outlook.Application")
Set Olmapi = Olapp.GetNamespace("MAPI")
'Open the inbox
Set Olfolder = Olmapi.GetDefaultFolder(olFolderInbox)
Set OlDelete = Olmapi.GetDefaultFolder(olFolderDeletedItems)
Set OlItems = Olfolder.Items
'Set up a loop to run till the inbox is empty (otherwise it skips some after
deleting)
InboxCounter = OlItems.Count
'For each mail in the collection check the subject line and process
accordingly
' Skip unread emails and ones with attachments
For i = InboxCounter To 1 Step -1
Set OlMail = Olfolder.Items(i)
If (OlMail.UnRead = False) And (OlMail.Attachments.Count = 0) Then
Rst.AddNew
Rst!FromName = OlMail.SenderName
Rst!ToName = OlMail.To
Rst!CCName = OlMail.CC
Rst!Subject = OlMail.Subject
Rst!SendDate = OlMail.ReceivedTime
Rst!Body = OlMail.Body
OlMail.Move OlDelete
Rst.Update
End If
InboxCounter = InboxCounter - 1
Next
Rst.Close
Set db = Nothing
Set Olapp = Nothing
Set OlItems = Nothing
Exit_CmdGetEmails_Click:
Exit Sub
Err_CmdGetEmails_Click:
MsgBox Err.Description
Resume Exit_CmdGetEmails_Click
End Sub
database. I can email information into a table, but I need to be able to
categorize them. I have combo boxes on a form that I would like to use as I
bring over each email so I can catalog them. I either need to figure out how
to show this form during my loop or set the form values to the values i bring
over and wait for me to hit "next" to import the next email. I thought that
I could import the data into the table and then show the userform (the
for/next loop waits until i finish adding the categories) then continues
on.... Here is the code I'm trying to get working.
1. Is it better to add a record and fill the form with the data i bring
over or try to add it to the table first, then show the form?
2. I'm not sure of the syntax for the do.cmd showform so i can edit data.
Private Sub CmdGetEmails_Click()
On Error GoTo Err_CmdGetEmails_Click
Dim Olapp As Outlook.Application
Dim Olmapi As Outlook.NameSpace
Dim Olfolder As Outlook.MAPIFolder
Dim OlDelete As Outlook.MAPIFolder
Dim OlMail As Object 'Have to late bind as appointments e.t.c screw it up
Dim OlItems As Outlook.Items
Dim Rst As DAO.Recordset
Dim db As DAO.Database
Dim InboxCounter As Integer
Set db = CurrentDb
Set Rst = db.OpenRecordset("tblEmail", dbOpenDynaset) 'Open table Email
DoCmd.GoToRecord , , acLast
'Create a connection to outlook
Set Olapp = CreateObject("Outlook.Application")
Set Olmapi = Olapp.GetNamespace("MAPI")
'Open the inbox
Set Olfolder = Olmapi.GetDefaultFolder(olFolderInbox)
Set OlDelete = Olmapi.GetDefaultFolder(olFolderDeletedItems)
Set OlItems = Olfolder.Items
'Set up a loop to run till the inbox is empty (otherwise it skips some after
deleting)
InboxCounter = OlItems.Count
'For each mail in the collection check the subject line and process
accordingly
' Skip unread emails and ones with attachments
For i = InboxCounter To 1 Step -1
Set OlMail = Olfolder.Items(i)
If (OlMail.UnRead = False) And (OlMail.Attachments.Count = 0) Then
Rst.AddNew
Rst!FromName = OlMail.SenderName
Rst!ToName = OlMail.To
Rst!CCName = OlMail.CC
Rst!Subject = OlMail.Subject
Rst!SendDate = OlMail.ReceivedTime
Rst!Body = OlMail.Body
OlMail.Move OlDelete
Rst.Update
End If
InboxCounter = InboxCounter - 1
Next
Rst.Close
Set db = Nothing
Set Olapp = Nothing
Set OlItems = Nothing
Exit_CmdGetEmails_Click:
Exit Sub
Err_CmdGetEmails_Click:
MsgBox Err.Description
Resume Exit_CmdGetEmails_Click
End Sub