Solved

VB6, Outlook Object Model Read Inbox

Posted on 2013-11-17
2
693 Views
Last Modified: 2013-11-20
Hi There,

We currently use the following code to read an Inbox to find a specific email based on the messageid.  Once found we extract information about the Outlook mail item like the subject, message text etc.  Pretty stock standard stuff.

            For iTemp = 1 To fol.Items.Count
                If fol.Items(iTemp).EntryID = sMessageId Then
                    'Ok, we have our first match.
                    sMessageId = fol.Items(iTemp).EntryID
                    'Bring back all the variables
                    sTo = fol.Items(iTemp).To
                    sCC = fol.Items(iTemp).CC
                    sBCC = fol.Items(iTemp).BCC
                    
                    sSubject = fol.Items(iTemp).subject

                    sText = fol.Items(iTemp).Body
                   sRecvDate = fol.Items(iTemp).ReceivedTime
                    sPriority = fol.Items(iTemp).Importance
                    sReadReceipt = IIf(fol.Items(iTemp).ReadReceiptRequested = False, 0, 1)
            
                    Set fol = Nothing
                    Set mainfolder = Nothing
            
                    ReadMessage = "SUCCESS"
                    GoTo EXITFUNCTION
                End If
            Next

Open in new window


As it is reading the properties of the email based on the number sequence it is found in the inbox - if a new email then arrives in the middle of this action - the wrong data then gets retrieved.  e.g  in the inbox I am reading email fol.Items(3).EntryID if another email then comes in, email 3 now becomes email 4 but the code is getting the properties of email 3 still which has just changed.

Is there a more robust way of doing this without the design flaw we have - seems a bit flaky if fol.items changes as emails come in ?

I recognise that changing the inbox view to sort ascending may resolve this but looking for a programmatical method because we cannot guarantee sort order.

Thanks in advance.
0
Comment
Question by:jcwiatr
2 Comments
 
LVL 5

Accepted Solution

by:
advfinance earned 500 total points
ID: 39656030
What about obtaining the item based on a certain index early on, then using this object throughout instead of always referring back to the collection?

For iTemp = 1 To fol.Items.Count

	Dim oMessage
	Set oMessage = fol.Items(iTemp)

	If oMessage.EntryID = sMessageId Then
		'Ok, we have our first match.
		sMessageId = oMessage.EntryID
		'Bring back all the variables
		sTo = oMessage.To
		sCC = oMessage.CC
		sBCC = oMessage.BCC
		
		sSubject = oMessage.subject

		sText = oMessage.Body
		sRecvDate = oMessage.ReceivedTime
		sPriority = oMessage.Importance
		sReadReceipt = IIf(oMessage.ReadReceiptRequested = False, 0, 1)

		Set fol = Nothing
		Set mainfolder = Nothing

		ReadMessage = "SUCCESS"
		GoTo EXITFUNCTION
	End If
Next

Open in new window


--
Chris
0
 
LVL 1

Author Comment

by:jcwiatr
ID: 39660638
Thanks Chris.  I will give it a try and post the results.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now