微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 VBA 显示 Windows 10 通知吐司

如何解决如何从 VBA 显示 Windows 10 通知吐司

我想知道在 Windows 10 中通过 VBA 显示通知 Toast 的最简单方法

我没有找到一个好的答案。我找到了一种从 PowerShell here 创建通知的非常简单的方法。但是我无法从 VBA 让它工作,因为我没有找到一种使用 WScript.Shell 在一行中执行它的方法

我尝试了几种方法来实现这一目标,但没有成功。您可以在下面找到我的最后一次尝试:

Public Sub Notify_test()

Dim WsShell     As Object: Set WsShell = CreateObject("WScript.Shell")
Dim strCommand  As String

strCommand = """powershell.exe"" ^ "
strCommand = strCommand & "[reflection.assembly]::loadwithpartialname(""System.Windows.Forms"")"
strCommand = strCommand & "; [reflection.assembly]::loadwithpartialname(""System.Drawing"")"
strCommand = strCommand & "; $notify = new-object system.windows.forms.notifyicon"
strCommand = strCommand & "; $notify.icon = [System.Drawing.SystemIcons]::information"
strCommand = strCommand & "; $notify.visible = $true"
strCommand = strCommand & "; $notify.showballoontip(10,""New Chat!"",""You have received New Chat!"",[system.windows.forms.tooltipicon]::None)"

WsShell.Run strCommand

End Sub

我想避免编写 .ps1 文件。有人可以帮我解决这个问题吗?

先谢谢你!

更新:

感谢@Remko,我能够显示通知

我做了一个函数来简化它的使用:

Public Function Notify(ByVal title As String,ByVal msg As String,_
                    Optional ByVal notification_icon As String = "Info",_
                    Optional ByVal app As String = "excel",_
                    Optional ByVal duration As Integer = 10)
                    
'Parameters:
'    title (str):Notification title
'    msg (str):Notification message
'    notification_icon (str):Notification icon. Available options are: Info,Error and Warning
'    app (str):Process name of app you want to be display in the system tray icon
'    duration (int):Duration of notification in seconds

Const PSpath    As String = "powershell.exe"

Dim WsShell     As Object: Set WsShell = CreateObject("WScript.Shell")
Dim strCommand  As String

If notification_icon <> "Info" And notification_icon <> "Error" And notification_icon <> "Warning" Then
    notification_icon = "Info"
End If

strCommand = """" & PSpath & """ -Command " & Chr(34) & "& { "
strCommand = strCommand & "Add-Type -AssemblyName 'System.Windows.Forms'"
strCommand = strCommand & "; $notification = New-Object System.Windows.Forms.NotifyIcon"
strCommand = strCommand & "; $path = (Get-Process -id (get-process " & app & ").id).Path"
strCommand = strCommand & "; $notification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)"
strCommand = strCommand & "; $notification.BalloonTipIcon  = [System.Windows.Forms.ToolTipIcon]::" & notification_icon & ""
strCommand = strCommand & "; $notification.BalloonTipText = '" & msg & "'"
strCommand = strCommand & "; $notification.BalloonTipTitle = '" & title & "'"
strCommand = strCommand & "; $notification.Visible = $true"
strCommand = strCommand & "; $notification.ShowBalloonTip(" & duration & ")"
strCommand = strCommand & " }" & Chr(34)

WsShell.Run strCommand,False

End Function

Public Sub Notify_Examples()

Notify "Insert Title Here","Insert Your Message Here"
Notify "Insert Title Here","Insert Your Message Here","Warning"
Notify "Insert Title Here","Error","outlook"

End Sub

我想进行额外的观察。离开 PSpath = "powershell.exe" 时没有任何反应。我想这是因为我的用户不是管理员。我能够通过将 powershell.exe 复制到我的文档并将 PSpath 更改为“C:\Users\my_user\Documents\powershell.exe”来绕过此问题。也许你也是这种情况...

解决方法

如果您坚持使用 PowerShell 执行此操作,则以下代码有效:

Public Sub Notify_Test()

Dim WsShell     As Object: Set WsShell = CreateObject("WScript.Shell")
Dim strCommand  As String

strCommand = "powershell.exe -Command " & Chr(34) & "& { "
strCommand = strCommand & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms')"
strCommand = strCommand & "; [reflection.assembly]::loadwithpartialname('System.Drawing')"
strCommand = strCommand & "; $notify = new-object system.windows.forms.notifyicon"
strCommand = strCommand & "; $notify.icon = [System.Drawing.SystemIcons]::Information"
strCommand = strCommand & "; $notify.visible = $true"
strCommand = strCommand & "; $notify.showballoontip(10,'New Chat!','You have received New Chat!',[system.windows.forms.tooltipicon]::None)"
strCommand = strCommand & " }" & Chr(34)
WsShell.Run strCommand

End Sub

它当然会短暂闪烁一个控制台(powershell)窗口

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?