A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

CDO Loop problem - Fix could be useful for many people.



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 30th 07, 04:45 PM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 1
Default CDO Loop problem - Fix could be useful for many people.

I have code that works for what I am doing but I need some help
cleaning it up. I am calling this procedure from a button on an
Access form. When the user clicks the button, the procedure loops
through all of the inbox mail and compares the sender addresses to the
email address in a textbox on the form. For each match, it adds the
body of the email to a subform on the Access form. I am storing
emails from people whose demographics are kept in the database.

Although my code works, it is tripping the Outlook OMG Security
warning twice. The first OMG is from starting the Outlook session,
which I use to loop through the Inbox folder. The second OMG is for
the CDO session I use to get the correct email address format of
" versus getting the sender returned as "John Doe"
via the Outlook session. Also, this code is slow. Now that we know
what I want to do....how can I fix it?

By the way....it would be cool to add a snippit that would loop
through emails only 6 months old vs the whole Inbox. This is not a
super-sophisticated procedure, but if I can get it figured out, many
people may find it helpful. Try to be specific in your response as my
coding skills are intermediate (not advanced). Thanks in advance for
help. Please also reply to sjstephens (at) bethesda.med.navy.mil

--Sam

Here is my code:

Private Sub CheckMail()
'Open Outlook
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim OlApp As Outlook.Application
Set OlApp = CreateObject("Outlook.Application")
Set fld =
OlApp.GetNamespace("MAPI").GetDefaultFolder(olFold erInbox)

'Start CDO session
Dim objSession As MAPI.Session
Dim objCDOMsg As MAPI.Message
Dim strEntryID As String
Dim strStoreID As String
Set objSession = CreateObject("MAPI.Session")
objSession.Logon , , False, False

'Declarations for Adding to Recordset
Dim db As DAO.Database
Set db = CurrentDb
Dim TempRst As DAO.Recordset
Set TempRst = CurrentDb.OpenRecordset("tblDirectAccessionsdetail ")

On Error Resume Next

'Get address from form for comparison to Inbox email during loop
Dim txtemail As String
txtemail = Me!DAEmail

'Loop Through Inbox
For Each itm In fld.Items

'Get information about email
strEntryID = itm.EntryID
strStoreID = itm.Parent.StoreID
Set objCDOMsg = objSession.GetMessage(strEntryID, strStoreID)
strAddress = objCDOMsg.Sender.Address

'Add to recordset
If txtemail = strAddress Then
With TempRst
.AddNew
!DAID = Me!DAID
!DAContactDate = objCDOMsg.TimeReceived
!DAContactMode = "Email"
!DAContactInit = "Individual"
!DAContactDetails = itm.Body
!DAInboxEntryID = objCDOMsg.EntryID
.Update
End With

On Error Resume Next

'Each time a record is added, refresh subform to reflect that
Forms!frmDirectAccessions!frmDirectAccessionsSubfo rm.Requery

Else
End If

Next itm

'Reset CDO session variables to nothing
Set objCDOMsg = Nothing
objSession.Logoff
Set objSession = Nothing

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err.Number = 429 Then
If appword Is Nothing Then
Set appword = CreateObject("word.Application")
Resume Next
End If
Else
MsgBox "Error No: " & Err.Number & "; Description: "
Resume ErrorHandlerExit
End If
End Sub

Ads
  #2  
Old March 30th 07, 04:52 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default CDO Loop problem - Fix could be useful for many people.

Your Outlook version is a key factor, but you forgot to mention it. That said, Redemption would give you an order of magnitude more speed and avoid security prompts.

If you want to stick with Outlook and see how to filter for a time period, see http://www.outlookcode.com/d/propsyntx.htm
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx

wrote in message oups.com...
I have code that works for what I am doing but I need some help
cleaning it up. I am calling this procedure from a button on an
Access form. When the user clicks the button, the procedure loops
through all of the inbox mail and compares the sender addresses to the
email address in a textbox on the form. For each match, it adds the
body of the email to a subform on the Access form. I am storing
emails from people whose demographics are kept in the database.

Although my code works, it is tripping the Outlook OMG Security
warning twice. The first OMG is from starting the Outlook session,
which I use to loop through the Inbox folder. The second OMG is for
the CDO session I use to get the correct email address format of
" versus getting the sender returned as "John Doe"
via the Outlook session. Also, this code is slow. Now that we know
what I want to do....how can I fix it?

By the way....it would be cool to add a snippit that would loop
through emails only 6 months old vs the whole Inbox. This is not a
super-sophisticated procedure, but if I can get it figured out, many
people may find it helpful. Try to be specific in your response as my
coding skills are intermediate (not advanced). Thanks in advance for
help. Please also reply to sjstephens (at) bethesda.med.navy.mil

--Sam

Here is my code:

Private Sub CheckMail()
'Open Outlook
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim OlApp As Outlook.Application
Set OlApp = CreateObject("Outlook.Application")
Set fld =
OlApp.GetNamespace("MAPI").GetDefaultFolder(olFold erInbox)

'Start CDO session
Dim objSession As MAPI.Session
Dim objCDOMsg As MAPI.Message
Dim strEntryID As String
Dim strStoreID As String
Set objSession = CreateObject("MAPI.Session")
objSession.Logon , , False, False

'Declarations for Adding to Recordset
Dim db As DAO.Database
Set db = CurrentDb
Dim TempRst As DAO.Recordset
Set TempRst = CurrentDb.OpenRecordset("tblDirectAccessionsdetail ")

On Error Resume Next

'Get address from form for comparison to Inbox email during loop
Dim txtemail As String
txtemail = Me!DAEmail

'Loop Through Inbox
For Each itm In fld.Items

'Get information about email
strEntryID = itm.EntryID
strStoreID = itm.Parent.StoreID
Set objCDOMsg = objSession.GetMessage(strEntryID, strStoreID)
strAddress = objCDOMsg.Sender.Address

'Add to recordset
If txtemail = strAddress Then
With TempRst
.AddNew
!DAID = Me!DAID
!DAContactDate = objCDOMsg.TimeReceived
!DAContactMode = "Email"
!DAContactInit = "Individual"
!DAContactDetails = itm.Body
!DAInboxEntryID = objCDOMsg.EntryID
.Update
End With

On Error Resume Next

'Each time a record is added, refresh subform to reflect that
Forms!frmDirectAccessions!frmDirectAccessionsSubfo rm.Requery

Else
End If

Next itm

'Reset CDO session variables to nothing
Set objCDOMsg = Nothing
objSession.Logoff
Set objSession = Nothing

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err.Number = 429 Then
If appword Is Nothing Then
Set appword = CreateObject("word.Application")
Resume Next
End If
Else
MsgBox "Error No: " & Err.Number & "; Description: "
Resume ErrorHandlerExit
End If
End Sub

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I fix my server problem? Jim Pickering Outlook Express 2 January 3rd 07 11:01 PM
How can I fix an Outlook problem? I need help Outlook - Installation 2 November 21st 06 06:11 PM
CDO & MailMessage problem Nader Outlook and VBA 2 May 18th 06 11:32 AM
Error 0X800CC18 how do I fix this problem? carl Outlook - Installation 1 March 22nd 06 09:06 PM
How do I fix login problem? Crystal Outlook - General Queries 1 February 27th 06 03:23 AM


All times are GMT +1. The time now is 05:27 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.