Solved

Outlook inbox

Posted on 2008-06-25
2
710 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 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