Solved

Outlook inbox

Posted on 2008-06-25
2
717 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

726 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