使用VBScript在Windows任务栏中显示新邮件图标

问题描述:

我很难配置Outlook以仅在需要时才显示新邮件图标.我设置了一些我不想显示的规则/过滤器,但它们总是这样做.我尝试了一切,但这不是我的问题.我发现一个好的解决方案是创建一个vbs脚本,该脚本称为bash脚本,用于通知我.我在全部规则中调用此代码,并让所有其他规则提早退出.效果很好.不过,我真正想要的是在任务栏中显示新的邮件图标(信封).我真的不了解vb,vba,vbs.但是,如果有人可以将我需要的代码发送到文件中,那么我很乐意试一试. 谢谢!

I was having the hardest time configuring outlook to show the new mail message icon only when I wanted it to. I have several rules/filters set up that I didn't want to show, but they always did. I tried everything, but that's not my question. What I found as a good solution was to create a vbs script that called a bash script I use to notify me. I call this code in my catch-all rule and have all the other rules exit early. It works pretty well. What I'd really like, though, is to display the new mail icon (envelope) in the taskbar. I'm really ignorant of vb, vba, vbs. But if someone can send the code that I need in a file, I'd love to give it a shot. THANKS!

据我所知,没有直接方法可以使用VBA显示 新邮件图标.但是,您可以按需添加 a 个其他任务栏图标.我敢肯定有一种方法可以通过使用LoadIcon或类似的Win32函数来使外观类似的图标出现,但是我还无法弄清楚该怎么做.

As far as I've been able to tell, there is no direct way to display the New Mail Icon using VBA. However, you can add a different tray icon on-demand. I'm sure there is a way to get a similar-looking icon to appear by using LoadIcon or a similar Win32 function, but I have not been able to figure out how.

请注意,这仅适用于32位Office(我无法使其在64位上运行;因此在这方面您很不走运-即使在

Note that this only works in 32-bit Office (I wasn't able to get it to work in 64-bit; so you're out of luck in that regard -- even in the Microsoft forums, that issue is unresolved. Then again, I think more highly of Stack Overflow than of the Microsoft Forums).

  1. 转到工具"->宏"->"Visual Basic编辑器",单击视图"->项目资源管理器".
  2. 在左侧项目"窗口上,右键单击"Project1",然后选择插入"->模块".
  3. 双击刚刚创建的新模块,

并粘贴以下代码:

'Some code borrowed from:
'http://support.microsoft.com/kb/176085

Public Type NOTIFYICONDATA
 cbSize As Long
 hwnd As Long
 uId As Long
 uFlags As Long
 uCallBackMessage As Long
 hIcon As Long
 szTip As String * 64
End Type

Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

Public Const IDI_APPLICATION = 32512&
Public Const IDI_ASTERISK = 32516&
Public Const IDI_EXCLAMATION = 32515&
Public Const IDI_HAND = 32513&
Public Const IDI_ERROR = IDI_HAND
Public Const IDI_INFORMATION = IDI_ASTERISK
Public Const IDI_QUESTION = 32514&
Public Const IDI_WARNING = IDI_EXCLAMATION
Public Const IDI_WINLOGO = 32517&

Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206

Public Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
    (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWndParent As Long, ByVal hwndChildAfter As Long, _
    ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Public Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As Long) As Long

Public nid As NOTIFYICONDATA

Public Sub ShowNotifyIcon()
    With nid
        .cbSize = Len(nid)
        .hwnd = 0
        'If you un-comment this line below the icon won't disappear when you mouse over it. You will need to use the HideNotifyIcon() function to make it disappear
        '.hwnd = FindWindowEx(0&, 0&, "mspim_wnd32", "Microsoft Outlook")
        .uId = vbNull
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .uCallBackMessage = WM_MOUSEMOVE

        .hIcon = LoadIcon(0&, IDI_APPLICATION)
        .szTip = "A message has arrived" & vbNullChar
       End With
       Shell_NotifyIcon NIM_ADD, nid
End Sub

Public Sub HideNotifyIcon()
    Shell_NotifyIcon NIM_DELETE, nid
End Sub

现在,要在Outlook规则中使用它们,您需要双击ThisOutlookSession,然后粘贴以下代码:

Now, to be able to use these in an Outlook rule, you need to double-click ThisOutlookSession, and paste the following code:

Public Sub ShowNewMailIcon(Item As Outlook.MailItem)
        Call ShowNotifyIcon
End Sub

Public Sub HideNewMailIcon(Item As Outlook.MailItem)
        Call HideNotifyIcon
End Sub

现在,您可以保存并关闭Visual Basic窗口.

Now you can save, and close the Visual Basic window.

要在规则中使用这些功能,则可以创建一个新规则:工具->规则和警报->新规则,在前两个屏幕上选择您的条件,然后在选择操作"屏幕上,选择运行脚本".当您将其添加到规则中并单击带下划线的运行脚本"时,应该会看到两个函数"ShowIconInTray"和"HideIconInTray".

To use these functions in a rule, you can then create a new rule: Tools->Rules and Alerts->New Rule, select your criteria on the first 2 screens, then on the "Select action(s)" screen, choose "run a script". When you add that to your rule, and click the underlined "run a script", you should then see the 2 functions "ShowIconInTray" and "HideIconInTray".

当您在规则中使用ShowIconInTray时,图标应在规则运行时出现,并且当您将鼠标悬停在其上时,图标应消失(我在为图标提供其他功能方面遇到了挑战,因为没有窗口句柄可以将其连接到可以接收并处理图标上的鼠标事件的位置.

When you use ShowIconInTray in your rule, the icon should appear when the rule runs, and when you mouse-over it, it should disappear (I was challenged in giving other functionality to the icon, because there is no window handle to connect it to that could receive and process the mouse events on the icon).

您可能需要检查Outlook的安全性(工具"->宏"->安全性").我认为Outlook 2007预先配置了很高的安全性.要使宏始终运行,可以选择不对宏进行安全性检查"或对宏的警告".签署VBA很容易,但超出了此答案的范围.

You may need to check Outlook's security (Tools->Macros->Security). I think Outlook 2007 comes preconfigured with high security. To get the macros to always run, you can select "No security check for macros" or "warnings for macros". Signing VBA is easy but beyond the scope of this answer.

这从来不是我最喜欢的代码,有些hacky;但是Shell_NotifyIcon并不是真正为在VBA中使用而设计的,并且您不能在VBScript中使用Win32函数.最好的替代答案可能包括VSTO加载项,但是您不能真正将加载项"粘贴到答案中,而且还需要Visual Studio.

This is not my favorite code ever, and it's somewhat hackish; but Shell_NotifyIcon wasn't really designed to be used in VBA, and you can't use Win32 functions in VBScript. The best alternative answer would probably include a VSTO add-in, but you can't really "paste" an add-in to an answer-- plus it would require Visual Studio.