如何从VBA显示Windows 10通知toast

如何从VBA显示Windows 10通知toast,vba,powershell,notifyicon,Vba,Powershell,Notifyicon,我想知道从VBA在windows 10中显示通知土司的最简单方法 我没有找到一个好的答案。我找到了一种从PowerShell创建通知的非常简单的方法。但我无法从VBA使其工作,因为我没有找到使用WScript.Shell在一行中执行此操作的方法 我尝试了几种方法来实现这一点,但没有成功。下面是我的最后一次尝试: Public Sub Notify_Test() Dim WsShell As Object: Set WsShell = CreateObject("WScript

我想知道从VBA在windows 10中显示通知土司的最简单方法

我没有找到一个好的答案。我找到了一种从PowerShell创建通知的非常简单的方法。但我无法从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, 0, 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", "Insert Your Message Here", "Error", "outlook"

End Sub
Public Function Notify(ByVal title作为字符串,ByVal msg作为字符串_
可选的ByVal通知图标为String=“Info”_
可选的ByVal应用程序为String=“excel”_
可选的ByVal持续时间(整数=10)
“参数:
'标题(str):通知标题
'msg(str):通知消息
'通知图标(str):通知图标。可用选项包括:信息、错误和警告
'应用程序(str):要显示在系统托盘图标中的应用程序的进程名称
'持续时间(int):通知的持续时间(秒)
Const PSpath As String=“powershell.exe”
将WsShell设置为对象:设置WsShell=CreateObject(“WScript.Shell”)
Dim strCommand As字符串
如果通知图标“信息”和通知图标“错误”以及通知图标“警告”,则
通知图标=“信息”
如果结束
strCommand=“”&PSpath&“-Command”&Chr(34)&“&{”
strCommand=strCommand&“添加类型-AssemblyName'System.Windows.Forms'”
strCommand=strCommand&“;$notification=newobjectsystem.Windows.Forms.NotifyIcon”
strCommand=strCommand&“;$path=(获取进程-id(获取进程“&app&”.id).path”
strCommand=strCommand&“;$notification.Icon=[System.Drawing.Icon]::ExtractAssociatedIcon($path)”
strCommand=strCommand&“$notification.balloottipicon=[System.Windows.Forms.ToolTipIcon]:”¬ification_icon&“
strCommand=strCommand&“;$notification.balloodtiptext=”&msg&“
strCommand=strCommand&“;$notification.balloodTiptTitle=”&title&“
strCommand=strCommand&“;$notification.Visible=$true”
strCommand=strCommand&“;$notification.showBalloodTip(&duration&)”)
strCommand=strCommand&“}”和Chr(34)
WsShell.runstrcommand,0,False
端函数
公共子系统通知示例()
通知“在此处插入标题”、“在此处插入邮件”
通知“在此处插入标题”、“在此处插入邮件”、“警告”
通知“在此处插入标题”、“在此处插入邮件”、“错误”、“outlook”
端接头

我想做一些额外的观察。离开PSpath=“powershell.exe”时不会发生任何事情。我想这是因为我的用户不是管理员。通过将powershell.exe复制到我的文档并将PSpath更改为“C:\Users\my\u 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)窗口

谢谢@Remko!成功了。我不坚持用PowerShell来做这件事,我只是觉得这样会更简单。如果你知道更好的方法,我真的很想知道