PDA

View Full Version : My code is executing ahead of outlook code!



dseeker
01-15-2016, 05:28 AM
Hello,


As you know the default setting for outlook 2010, at least the setup I have, is that a newly received (and selected) email is marked as 'read' once the selection is changed by the user.


I wanted to use this event in my VBA code to prompt the user to Save the email that has just been marked as read. The code below works in the sense that it does what is supposed to do every time the 'ActiveExplorer.Selection' changes. However, the problem I'm facing is that it is always executed BEFORE outlook has the time to mark the previous emails as read!.


I tried to introduce a time pause after the user makes a new selection, but this pause seems to halt all outlook activities not only my code!!


My question is: do you have any idea how make my code execute AFTER outlook has finished marking the previously selected email as READ??


Here is my code if you want to have a look




Public WithEvents myOlExp As Outlook.Explorer
Dim Flag As Integer
Dim oMail As Outlook.MailItem
Private Sub Application_Startup()
Dim objItem As Object
Set myOlExp = Application.ActiveExplorer
enviro = CStr(Environ("USERPROFILE"))


For Each objItem In myOlExp.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem
End If
Next
If oMail.UnRead Then
Flag = 1
Else
Flag = 0
End If
End Sub

Private Sub myOlExp_SelectionChange()


'If oMail.UnRead Then
'Flag = 0
'Else
'Flag = 1
'End If
MsgBox "Flag is "&Flag
If Flag = 1 Then
MsgBox Flag
If oMail.UnRead = False Then
MsgBox "previous email has just been read do you want to save"
Else
MsgBox "Previous email still marked as unread, do nothing"
End If


For Each objItem In myOlExp.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem
Else
MsgBox "not an email selection"
End If
Next
If oMail.UnRead Then
Flag = 1
MsgBox "you now selected an unread email"
Else
Flag = 0
MsgBox "you now selected an already read email"
End If

Else
MsgBox Flag & " The Previous email was already open anyway"
For Each objItem In myOlExp.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem
End If
Next
If oMail.UnRead Then
Flag = 1
Else
Flag = 0
End If
MsgBox "Flag is " & Flag
End If

End Sub