Solved

Outlook inbox

Posted on 2008-06-25
2
712 Views
Last Modified: 2008-07-05
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
0
Comment
Question by:wwstudioinc
2 Comments
 
LVL 23

Accepted Solution

by:
irudyk earned 500 total points
ID: 21866264
I've used the following code to get the count of unread emails within the Inbox and its subfolders.  Seems to work whether or not the user's Outlook is open or not (if it's not, then the code pauses as I guess it's attempting to open the users Outlook mailbox in the background).  Not sure if it'll solve your issue, but the code seems a little bit more compact and seems to always bring in the correct count for me.
Public Unread_Emails As Long
 

Function UnreadMail()
 

Dim olNameSpace As Object

Dim olFolder As Object

'if the Outlook library reference is being used

'Dim olNameSpace As Outlook.NameSpace

'Dim olFolder As Outlook.MAPIFolder
 

Unread_Emails = 0
 

Set olNameSpace = GetObject("", "Outlook.Application").GetNamespace("MAPI")
 

Call GetFolderEmailCount(olNameSpace.GetDefaultFolder(6))

'if the Outlook library reference is being used

'Call GetFolderEmailCount(olNameSpace.GetDefaultFolder(olFolderInbox))
 

Set olNameSpace = Nothing

Debug.Print Unread_Emails
 

End Function
 

Function GetFolderEmailCount(olFolderName As Object)

'if the Outlook library reference is being used

'Sub GetFolderEmailCount(olFolderName As Outlook.MAPIFolder)
 

Dim i As Long
 

If olFolderName.Folders.Count > 0 Then
 

    Unread_Emails = Unread_Emails + olFolderName.UnReadItemCount
 

    For i = 1 To olFolderName.Folders.Count

        Call GetFolderEmailCount(olFolderName.Folders(i))

    Next i
 

Else
 

    Unread_Emails = Unread_Emails + olFolderName.UnReadItemCount
 

End If
 

End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:jjafferr
ID: 21866560
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
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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

930 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

12 Experts available now in Live!

Get 1:1 Help Now