> the whole article is here:
http://www.mvps.org/access/queries/qry0013.htm
I looked at that site - that is definitely my problem. What I've done in
the mean time is use a MakeTable query to dump the query results into a
table, then define the recordset based on the table. I'm not sure how much
of a performance hit I'm taking, but it seems to be working. See complete
code below.
What this sub does is loop through every message in the Outlook Sent Items
folder and populates a table with DateSent, Subject and Recipient of every
message that was sent to each contact in the mdb (there's a button on the
mdb's Contacts form to show the results - ad hoc in a popup form -
individually for each contact. It would take too long to do every contact
at once).
The joker in the pack is the limitation of the Outlook Object Model: there
is no way to always get a fully qualified email address out of the MailItem
property. It depends on how the contact is saved in the Outlook Contacts
folder - if the "DisplayAs" field contains the email address, you can
usually get the email address out of the "MailItem.T o" property for all the
messages sent to that contact; but if you've just replied to a message,
sometimes "MailItem.T o" has only the contact's name, e.g. 'John Smith' -
this means my code would not find that message - because I am looking for
the contact's email address.
Public Sub SentMessages()
On Error GoTo HandleErr
Dim rst, rste As DAO.Recordset
Dim db As DAO.Database
Dim olns As Outlook.Namespa ce
Dim ola As New Outlook.Applica tion
Dim olfsm As Outlook.MAPIFol der
Dim olmi As Outlook.MailIte m
Dim strEmail, j, i As String
DoCmd.Hourglass True
DoCmd.SetWarnin gs False
DoCmd.OpenQuery "qryEmailSentDe lete"
DoCmd.OpenQuery "qryEmailEntity " 'qdf here...
DoCmd.SetWarnin gs True
Set db = CurrentDb
Set olns = ola.GetNamespac e("MAPI")
Set olfsm = olns.GetDefault Folder(olFolder SentMail)
Set rst = db.OpenRecordse t("tblEmailSent ")
Set rste = db.OpenRecordse t("tblEmailEnti ty") 'use qdf here to avoid
table creation?
Do Until rste.EOF
If InStr(1, rste!s, "#", vbTextCompare) Then 'ignore comments next
to email address
j = InStr(1, rste!s, "#", vbTextCompare)
i = "1"
Else
j = Nz(Len(rste!s), 0)
i = "0"
End If
strEmail = Left(rste!s, j - i)
'Debug.Print "strEmail = " & strEmail
For Each olmi In olfsm.Items
DoEvents
'Debug.Print "unmatched olmt.to = " & olmi.To
If (InStr(olmi.To, strEmail)) > 0 Then
rst.AddNew
rst!Sent = (CDate(olmi.Sen tOn))
rst!Subject = olmi.Subject
rst!Recipient = olmi.To
'Debug.Print "matched olmi.To = " & olmi.To
rst.Update
End If
Next
rste.MoveNext
Loop
Exit_here:
DoCmd.Hourglass False
rst.Close
rste.Close
Set olns = Nothing
Set olfsm = Nothing
Set rst = Nothing
Set rste = Nothing
Set db = Nothing
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogE rr ("modOutlook(Se ntMessages)")
Resume Exit_here
End Select
End Sub