PDA

View Full Version : Solved: UserForm Refresh?



wildpianist
01-21-2008, 06:13 AM
I have a some code that tells me how many unread email messages I have in my Outlook Mailbox.

With my system here, if an email comes in it instantly appears on the bottom right as an envelope. Is there a way to get the code to refresh say every second. Would there be a way to do this without it flickering?

wildpianist
01-21-2008, 07:38 AM
the

wildpianist
01-21-2008, 07:38 AM
the code in the userform that is

rory
01-21-2008, 07:40 AM
Which version of Outlook do you have? The Outlook Application does have a NewMail event that you can trap, but if you get multiple messages in in close succession, it doesn't always fire for each item. You can also trap the ItemAdd avent of a MAPIFolder's Items collection which may be more reliable.
Alternatively, you can use Application.OnTime to run your macro every minute or so. I'm not sure what you are saying is flickering?

wildpianist
01-21-2008, 07:52 AM
Outlook version I have is 2003.

I have a userform that has buttons that loads up IE with webpages and at the bottom it has "Unread Emails - Whatever" but it wont update unless the userform is exited and re-run. But If I had it to refresh every second it surely would flicker because its refreshing. Unless there is another way.

:)

wildpianist
01-21-2008, 08:41 AM
what would be the code to put this in a userform

I basically just have a label caption that I am using.

rory
01-21-2008, 09:02 AM
Create a new class module in your project, name it COutlookEvents, set a reference to the Outlook object library in your project (if you don't already have one) and paste this in:
Option Explicit
Public WithEvents colItems As Outlook.Items
Public Event NewItemCount(lngItemCount As Long)
Private Sub colItems_ItemAdd(ByVal Item As Object)
RaiseEvent NewItemCount(colItems.Parent.UnReadItemCount)
End Sub
Private Sub colItems_ItemChange(ByVal Item As Object)
RaiseEvent NewItemCount(colItems.Parent.UnReadItemCount)
End Sub


then your userform needs code like this (assumes Label1 is the control that has the message count):
Option Explicit
Private WithEvents cevtOL As COutlookEvents
Private Sub cevtOL_NewItemCount(lngItemCount As Long)
Me.Label1.Caption = lngItemCount & " unread items"
End Sub

Private Sub UserForm_Initialize()
Dim fdr As Outlook.MAPIFolder
Set cevtOL = New COutlookEvents
Set fdr = CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6)
Me.Label1.Caption = fdr.UnReadItemCount & " unread items"
Set cevtOL.colItems = fdr.Items
End Sub

HTH

wildpianist
01-21-2008, 09:27 AM
Wow!!! That is absolutely fantastic!!! I didn't think it could be done!
But I think Outlook has to be open, bercause I tried it with it shut and got "The Operation Failed!"

rory
01-21-2008, 09:37 AM
It should try to open Outlook if it's not open, but if there is a problem with that for any reason - e.g. a login prompt - then it may well fail.

wildpianist
01-21-2008, 09:53 AM
Hmmmm its weird, If outlook is closed and I get an email that the operation had failed. But if I close the userform and re-run it still with outlook closed it then says there's 1 Unread Email Hmmmm

wildpianist
01-21-2008, 09:55 AM
Ahhh - on running the userform with Outlook closed I noticed Outlook on the TaskBar flash up and then quickly disappear.

wildpianist
01-21-2008, 09:56 AM
This is the code that says the operation had failed on:-


Private Sub colItems_ItemChange(ByVal Item As Object)
RaiseEvent NewItemCount(colItems.Parent.UnReadItemCount)
End Sub

rory
01-21-2008, 10:00 AM
Well, yes, if you close Outlook after opening the form, then it has lost its reference to Outlook. Is this something you want to be able to do?

wildpianist
01-21-2008, 10:11 AM
is it possible?

rory
01-21-2008, 10:23 AM
I'm not sure off the top of my head - as a matter of interest why do you want to close Outlook rather than just minimizing it?

wildpianist
01-21-2008, 10:50 AM
I could do. I guess it doesn't really matter.

Thanks for all your kind help.

kostasfer
12-28-2011, 12:54 AM
old