簡體   English   中英

如何顯示來自 VBA 的 Windows 10 通知 toast

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

我想知道在 VBA 的 windows 10 中顯示通知 toast 的最簡單方法。

我沒有找到一個好的答案。 我在這里找到了一種從 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

我想避免編寫 a.ps1 文件。 有人可以幫我解決這個問題嗎?

先感謝您!

更新:

感謝@Remko,我能夠顯示通知。

我做了一個 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

我想做些額外的觀察。 離開 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

它當然會簡要介紹 flash 控制台(powershell) window

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM