Can anyone help me with notifyicon in vba. I am able to put an icon in the system tray, but I can't capture mouse movements. I am using it in Excel when I hide Excel and I want to be able to show Excel when the icon is clicked. Here is the code that I have so far.
'Option Explicit
' Type passed to Shell_NotifyIcon
Private Type NOTIFYICONDATA
Size As Long
Handle As Long
ID As Long
Flags As Long
CallBackMessage As Long
Icon As Long
Tip As String * 64
End Type
' Constants for managing System Tray tasks, foudn in shellapi.h
Private Const AddIcon = &H0
Private Const ModifyIcon = &H1
Private Const DeleteIcon = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const MessageFlag = &H1
Private Const IconFlag = &H2
Private Const TipFlag = &H4
Private Declare Function Shell_NotifyIcon _
Lib "shell32" Alias "Shell_NotifyIconA" ( _
ByVal Message As Long, Data As NOTIFYICONDATA) As Boolean
Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Dim Data As NOTIFYICONDATA
Private Sub AddIconToTray()
If stat = 0 Then
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image1.Picture
Data.Tip = "IM Tools Pushmaster Process" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
ElseIf stat = 100 Then
DeleteIconFromTray
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image2.Picture
Data.Tip = "IM Tools Pushmaster Process Complete" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
Else
DeleteIconFromTray
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image1.Picture
Data.Tip = stat & "% - IM Tools Pushmaster Process" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
End If
End Sub
Private Sub DeleteIconFromTray()
Call Shell_NotifyIcon(DeleteIcon, Data)
End Sub
Private Sub UserForm_Resize()
If Me.Height = 102 Then
DeleteIconFromTray
Else
AddIconToTray
End If
End Sub
Private Sub UserForm_Terminate()
DeleteIconFromTray
End Sub
'Option Explicit
' Type passed to Shell_NotifyIcon
Private Type NOTIFYICONDATA
Size As Long
Handle As Long
ID As Long
Flags As Long
CallBackMessage As Long
Icon As Long
Tip As String * 64
End Type
' Constants for managing System Tray tasks, foudn in shellapi.h
Private Const AddIcon = &H0
Private Const ModifyIcon = &H1
Private Const DeleteIcon = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const MessageFlag = &H1
Private Const IconFlag = &H2
Private Const TipFlag = &H4
Private Declare Function Shell_NotifyIcon _
Lib "shell32" Alias "Shell_NotifyIconA" ( _
ByVal Message As Long, Data As NOTIFYICONDATA) As Boolean
Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Dim Data As NOTIFYICONDATA
Private Sub AddIconToTray()
If stat = 0 Then
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image1.Picture
Data.Tip = "IM Tools Pushmaster Process" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
ElseIf stat = 100 Then
DeleteIconFromTray
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image2.Picture
Data.Tip = "IM Tools Pushmaster Process Complete" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
Else
DeleteIconFromTray
Data.Size = Len(Data)
Data.Handle = FindWindow("ThunderDFrame", Form.Caption) 'Application.Hwnd
Data.ID = vbNull
Data.Flags = IconFlag Or TipFlag Or MessageFlag
Data.CallBackMessage = WM_LBUTTONDBLCLK
Data.Icon = Image1.Picture
Data.Tip = stat & "% - IM Tools Pushmaster Process" & vbNullChar
Call Shell_NotifyIcon(AddIcon, Data)
End If
End Sub
Private Sub DeleteIconFromTray()
Call Shell_NotifyIcon(DeleteIcon, Data)
End Sub
Private Sub UserForm_Resize()
If Me.Height = 102 Then
DeleteIconFromTray
Else
AddIconToTray
End If
End Sub
Private Sub UserForm_Terminate()
DeleteIconFromTray
End Sub