PDA

View Full Version : New mail notification



mvidas
11-02-2006, 06:55 AM
Hi all,

I occasionally either have my taskbar hidden or a full-screen game on so I can't always see the little envelope in there from outlook to tell me I have new mail. So I decided to use something else to catch my eye, and decided on the scroll lock light (a blinking light catches my eye much more than an icon in the taskbar; I didn't want a popup or anything like that).

So I wrote one. Anyone think this would be useful?
Also, testing in a different version than outlook 2k sr1 would be appreciated :)

You'll need to use the ThisOutlookSession class module, as well as a new class module called "vBlinkerCls", and a standard code module.

ThisOutlookSession:
'***** Start ThisOutlookSession code *****
Option Explicit
Dim BlinkMail As vBlinkerCls
Sub Application_Startup()
Set BlinkMail = New vBlinkerCls
End Sub
Private Sub Application_Quit()
Set BlinkMail = Nothing
End Sub
'***** End ThisOutlookSession code *****

vBlinkerCls:
'***** Start vBlinkerCls code - Class Module *****
Option Explicit
Private WithEvents ThisApp As Outlook.Application
Private WithEvents ActivExplorer As Outlook.Explorer
Public HasFocus As Boolean
Public ScrollOn As Boolean
Private Const BLINK_MILLISECONDS As Long = 500
Private Const VK_SCROLL = &H91
Private Sub ActivExplorer_Activate()
HasFocus = True
End Sub
Private Sub ActivExplorer_Deactivate()
HasFocus = False
End Sub
Private Sub ThisApp_NewMail()
If Not HasFocus Then
StartFlash
Do Until HasFocus
DoEvents
Loop
StopFlash
End If
End Sub
Sub StartFlash()
If Not KeepGoing Then
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
ScrollOn = keys(VK_SCROLL)
End If
KeepGoing = True
SetTimer 0&, 0&, BLINK_MILLISECONDS&, AddressOf PressScrollLock
End Sub
Sub StopFlash()
KeepGoing = False
DoEvents
Sleep BLINK_MILLISECONDS
DoEvents
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
If keys(VK_SCROLL) <> ScrollOn Then
keybd_event VK_SCROLL, &H45, 0, 0
keybd_event VK_SCROLL, &H45, &H2, 0
End If
End Sub
Private Sub Class_Initialize()
Set ThisApp = Application
Set ActivExplorer = ThisApp.ActiveExplorer
End Sub
Private Sub Class_Terminate()
Set ActivExplorer = Nothing
Set ThisApp = Nothing
End Sub
'***** End vBlinkerCls code *****

Standard code module:
'***** Start vBlinkerMdl code - Standard Module *****
Option Explicit
Private Const VK_SCROLL = &H91
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan _
As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal _
nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal _
nIDEvent As Long) As Long
Public KeepGoing As Boolean
Sub PressScrollLock(ByVal hWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, _
ByVal dwTimer As Long)
If KeepGoing Then
keybd_event VK_SCROLL, &H45, 0, 0
keybd_event VK_SCROLL, &H45, &H2, 0
End If
If Not KeepGoing Then KillTimer 0&, nIDEvent
End Sub
'***** End vBlinkerMdl code *****

Also attaching exported versions of these
Any thoughts/suggestions? It works for me as I need it but others might have a better idea or a better way of doing some of it.

Matt

TheAntiGates
11-29-2006, 10:57 AM
Runs perfectly with OL03 on XP.

This is useful - thanks. Expect code snips to live long and procreate - I've already cloned/cannibalized from it substantially. :bow:

TheAntiGates
12-05-2006, 01:20 PM
That is a great routine, but since using it I have had trouble with [nonOutlook usage of] the alt-, control- and shift-functionality (e.g., things will behave as though I was holding down the shift key). I don't know if it is coincidental, or is related to the scroll lock toggling? I wonder if the DoEvents loop is the culprit, or the timer code invocation simultaneously to my holding the (e.g.) shift key...

I did attempt to make some tweaks, one of which involved using {GetKeyboardState keys(0)} in the Timer code. However keys(VK_SCROLL) would regularly then be 0 even though scroll light was on, verified by setting a stop after GetKeyboardState. If I "Set Next Statement" to immediately reperform GetKeyboardState, stepping the code would change it to 1 ! I have often had problems with Timer code ... can someone shed some light on the limitations or restictions there? (Using commands like doEvents or Stop or msgbox in Load events or timer code seem to cause problems - is this related?)

By the way I don't know if it's kid stuff for "real men" event code programmers, but I am impressed with the Sleep trick to allow one final timer fire, in order to kill itself. I don't know if it was just cute, or purposeful in that the timer had a final itch needing to be scratched. AAR my uninspired inclination would have been to just call the timer code directly :(

mvidas
02-08-2007, 10:08 AM
That is a great routine, but since using it I have had trouble with [nonOutlook usage of] the alt-, control- and shift-functionality (e.g., things will behave as though I was holding down the shift key). I don't know if it is coincidental, or is related to the scroll lock toggling? I wonder if the DoEvents loop is the culprit, or the timer code invocation simultaneously to my holding the (e.g.) shift key...

Hmmmm.. the only thing I dont love about it is when I'm typing something into excel without looking and the scroll lock screws me up (and I dont notice it for a little while). There shouldn't be any difference in functionality of alt/ctrl/shift from the code alone, though it could just be you're having those issues with scroll lock being on :dunno


I did attempt to make some tweaks, one of which involved using {GetKeyboardState keys(0)} in the Timer code. However keys(VK_SCROLL) would regularly then be 0 even though scroll light was on, verified by setting a stop after GetKeyboardState. If I "Set Next Statement" to immediately reperform GetKeyboardState, stepping the code would change it to 1 ! I have often had problems with Timer code ... can someone shed some light on the limitations or restictions there? (Using commands like doEvents or Stop or msgbox in Load events or timer code seem to cause problems - is this related?)
Using timers can be dangerous! That was one huge benefit of excel's .OnTime method, in that if vba or the system is busy, it delays the timer trigger. Using the API timers like this can be bad, especially if the VBA that called it is busy when its supposed to be triggered... BOOM the app crashes. Annoying, but as long as you know what to expect from it and work around those limitations, it can be quite useful.


By the way I don't know if it's kid stuff for "real men" event code programmers, but I am impressed with the Sleep trick to allow one final timer fire, in order to kill itself. I don't know if it was just cute, or purposeful in that the timer had a final itch needing to be scratched. AAR my uninspired inclination would have been to just call the timer code directly :(I think I put it in there because the scroll lock light wouldn't necessarily revert back to it's original state without it? I don't exactly remember why, but I believe that was it.

I made a com add-in version of this as well if you're interested, I don't have it with me at the moment but I do have it at home

Zephid15
05-02-2007, 08:02 AM
this does not work for me. it sounds really useful but i cant get it to work.
Dave

TheAntiGates
05-02-2007, 11:21 AM
It's fantastic, but it does interfere with other apps :(

mvidas
05-08-2007, 06:08 AM
Zephid,

What version of outlook do you use? It should work in all but 2007 (and I have no idea if it would work there--havent even tried that yet--but im guessing not). Are you getting any kind of errors or anything?

I do agree that it can interfere with other apps, we have a homemade one here at work which the scroll lock blinking tends to mess up a couple data entries. I wish keyboards had a 'notification' light that had no other purpose (then again, if they did then i wouldnt need to write anything like this as MS would likely incorporate something like it anyways).

I just tried looking for my COM addin version here at work, and dont seem to have it. I would have sworn I brought it in back in March to send to Dick Kusleika, but I guess not. When my PC at home is back up and running (couple days hopefully) I'll get that together and upload a copy of it here, maybe just the VBA version isn't working for you (the COM is much easier and doesnt randomly flip to outlook vba at times either.. :shifty: )

Matt

Zephid15
05-09-2007, 09:52 AM
i am using office 2003, and i pasted the code in the the corisponding places. i am getting no error message. when i attempted to send a message to my self nothing happened.

mvidas
05-09-2007, 11:19 AM
Hmm... good question Dave! A couple more questions..
Did you close/reopen outlook? (I'm sure you have since you first posted that). Originally I wrote it so it creates the BlinkMail variable when outlook is opened, though I have since added (this to) the _NewMail event, so it makes sure it is working:Private Sub Application_NewMail()
If BlinkMail Is Nothing Then
Set BlinkMail = New vBlinkerCls
Call BlinkMail.NewMail
End If
End SubThat would also go into the ThisOutlookSession object.

Another thing to note is that it only blinks if the Outlook application doesn't have focus. So when testing, create the new message, change focus to anything but outlook (so when the message is sent, focus doesnt return to outlook), switch back to the message, then send it.

If still no joy, have you verified that VBA is allowed to run (Tools/Macro/Security)?