Link to home
Start Free TrialLog in
Avatar of wwstudioinc
wwstudioinc

asked on

Outlook inbox

I am using the code listed below on a form ontimer event to check outlook inbox for new unread emails.What i have realize is that if fails to return the correct inbox count what i have to do is to open outlook once I close outlook then the correct inbox count is returned.My question is there a was to requery or resync outlook inbox or is there a problem with the code listed below.thanks


    Dim objFolder As Redemption.RDOFolder

    Dim oItem As Redemption.RDOMail
Dim oSession As Redemption.rdoSession
Dim mOutlookApp As Outlook.Application
    Dim StrMessage As String
     Dim StrflagStatus As Integer
    Dim New_Mail As Integer
    Static MytimeFlash As Date
    Static In_here As Integer
    Static Inhere As Integer
    Static bln As Integer
    Static Un_Read_Count As Integer
    Static oInbox As Object
    Static objFolder_Junk  As Object

      '/// no need to set oMAPI if it has a value already
      On Error Resume Next
     Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
    Err.Clear
    Set mOutlookApp = CreateObject("Outlook.application")
    End If
    Do_Events
       
    If IsNothing(oNameSpace) Then
        Set oNameSpace = mOutlookApp.GetNamespace("MAPI")
       ' Set oNameSpace = GetObject _
                    ("", "Outlook.application").GetNamespace("MAPI")
            End If
Set oInbox = oNameSpace.Folders(1)

      Select Case oInbox.Name
  Case "MSN", "Hotmail"
   Case Else
    Set oInbox = GetFolder_Name(oMAPI, "Mailbox")
  End Select
    Set oOutlook = Nothing

   
    If IsNothing(oSession) Then _
       Set oSession = New Redemption.rdoSession
    On Error Resume Next
    oSession.Logon , , , False, False

    If IsNothing(objFolder) Then
       Set objFolder = oSession.GetFolderFromPath("\\" & oInbox & "\Inbox")
        Set objFolder_Junk = oSession.GetFolderFromPath("\\" & oInbox & "\Junk E-Mail")
        End If
         '// count messages
          Unread_Emails = 0
    Unread_Emails = objFolder.UnReadItemCount
     Unread_Emails = Unread_Emails + objFolder_Junk.UnReadItemCount
    Set oItems = objFolder.Items.Restrict("[UnRead]='True'")

    For Each oItem In oItems
        If InStr(1, Message_Read, CStr(oItem.EntryID)) = 0 Then
            Message_Read = Message_Read & CStr(oItem.EntryID) & "-"

            '// /subject
            sSubject = "The original message was received on" & Chr(32) & _
            oItem.ReceivedTime & vbCrLf & "From:" & oItem.Sender & vbCrLf & "To:" & oItem.To & vbCrLf & "Subject:" & oItem.Subject
            '// /Priority
            sPriority = oItem.Importance
            '//// flag statu
                StrflagStatus = oItem.FlagStatus
                'Debug.Print StrflagStatus
            '/// checks for attachments
            strAttIcon = "unread"
            If Not oItem.Attachments.Count = 0 Then
                Set Attach_ment = oItem.Attachments.item(1)
                strAttIcon = "watt'"
            End If

            '/////////////////////////////////////////////////////////////////////////
            If Can_cel = True Then GoTo Wes_Continue
           
         
            If IsFormOpen("SplashMail") Then

            Else
                DoCmd.OpenForm "SplashMail", acNormal, , , , acHidden
            End If
            Pause 1
            'DoEvents
            Set frm = Forms!SplashMail
            frm.lblHeader.Caption = "Received"
            frm.Visible = True
            If Len(strPriority) = 0 Then strPriority = 2
            frm.lblpriority.Visible = Not Len(strPriority) = 0
            If Len(strPriority) > 0 Then
                Select Case strPriority
                Case 1
                    frm.lblpriority.Caption = "!"
                    frm.lblpriority.ForeColor = vbRed
                Case 2
                    frm.lblpriority.Caption = ""
                End Select
            End If
            frm.ImgMail.Picture = CurrentProject.Path & "\MSOutlook\Outlookicons\" & strAttIcon & ".bmp"
            frm.Setup sSubject

Wes_Continue:
        End If


    Next


    On Error Resume Next

    Check_Emails_Redemption = Unread_Emails
    oSession.Logoff
    Set oSession = Nothing
Set objFolder = Nothing
Set objFolder_Junk = Nothing
'objOutlook.Quit
 Set objOutlook = Nothing
'// end of checking emails
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

instead of restricting the loop for the UnRead emails (which might be causing the problem),
make the loop for all the emails, but make your code go through the UnRead ones, like this:

if oItem.UnRead = false then
  go to the loop, to read the next email.
else
 go through your normal code


jaffer