?
Solved

Outlook inbox

Posted on 2008-06-25
2
Medium Priority
?
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
irudyk earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

765 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