Skip to content

Instantly share code, notes, and snippets.

@jeff123wang
Created April 18, 2022 15:58
Show Gist options
  • Save jeff123wang/bc78a1e47607b77c84692d8ba02e4986 to your computer and use it in GitHub Desktop.
Save jeff123wang/bc78a1e47607b77c84692d8ba02e4986 to your computer and use it in GitHub Desktop.
this will show non blocking notification using powershell in vba
'https://stackoverflow.com/questions/65680941/powershell-unable-to-find-type-windows-ui-notification
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment