简体   繁体   English

如何显示来自 VBA 的 Windows 10 通知 toast

[英]How to show Windows 10 notification toast from VBA

I'd like to know the easiest way to show notification toast in windows 10 from VBA.我想知道在 VBA 的 windows 10 中显示通知 toast 的最简单方法。

I didn't found a good answer to this.我没有找到一个好的答案。 I found a really simple way to create notifications from PowerShell here .我在这里找到了一种从 PowerShell 创建通知的非常简单的方法。 But I can't get it to work from VBA because I didn't find a way to execute this in one line using WScript.Shell.但是我无法从 VBA 开始工作,因为我没有找到一种方法来使用 WScript.Shell 在一行中执行它。

I tried several ways to accomplish that but i didn't succeed.我尝试了几种方法来实现这一点,但我没有成功。 Below you can find my last attempt:您可以在下面找到我的最后一次尝试:

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

I'd like to avoid writing a.ps1 file.我想避免编写 a.ps1 文件。 Can someone please help me with this question?有人可以帮我解决这个问题吗?

Thank you in advance!先感谢您!

UPDATE:更新:

Thanks to @Remko I was able to display the notification.感谢@Remko,我能够显示通知。

I did a function to make its use simpler:我做了一个 function 以使其使用更简单:

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

I'd like to make and extra observation.我想做些额外的观察。 Nothing happens when leave the PSpath = "powershell.exe".离开 PSpath = "powershell.exe" 时没有任何反应。 I guess this is due to the fact my user is not an admin.我想这是因为我的用户不是管理员。 I was able to bypass this by copying the powershell.exe to my documents and altering the PSpath to "C:\Users\my_user\Documents\powershell.exe".我可以通过将 powershell.exe 复制到我的文档并将 PSpath 更改为“C:\Users\my_user\Documents\powershell.exe”来绕过这个问题。 Maybe this is the case for you too...也许你也是这种情况...

If you insist doing this with PowerShell the following code works:如果您坚持使用 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

It will of course briefly flash a console (powershell) window它当然会简要介绍 flash 控制台(powershell) window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM