How to make Toaster Notification on Excel with VBA - excel

I would like to create/use toasters notifications on Excel. Because we already use MsgBox to notify the user that something happen.
But it make the script to stop (pause).
Have you any idea of how to do ?
On google there is "System Tray Notification" but it need a lot of code and this is a old method. Can't find if there is a new method.
For example, the plugin from SAP : "Analysis For Office" put notifications on Excel.

I looked into this once and almost gave up on it entirely until I found a rather 'cheaty' way of alerting users of info without suspending execution, that works on my users machines.
We run Windows 10 here and have SCCM installed for software distribution and updates. I've absolutely no idea if that's mandatory in Windows for updates or not, so I've no idea if this works for you.. but the following code works a treat here if you don't mind the notification resembling a Software Centre notification:
Sub Toastnote(ccmTitle, ccmText)
Shell "c:\windows\ccm\sctoastnotification.exe """ & ccmTitle & """ """ & ccmText & """ "
End Sub
You can call it with:
toastnote "title goes here","message goes here"
It creates a little pop-up that looks like this:
As I say, it's a bit of a cheat and might confuse users who regularly receive CCM notifications, but for my user-base that wasn't an issue.
Lastly, it's probably worth wrapping this in an IF statement that checks the .exe file exists - just a thought..

Using the Plugin "Analysis For Office" from SAP, you can define messages and add them to the standard SAP-AnalysisForOffice message dialog :
Dim lResult As Long
lResult= Application.Run("SAPAddMessage", "This is a new error message!", "ERROR")
The message 'This is a new error message' with severity Error is displayed in the message dialog.
It will do the same as the picture sent with my question.
Source
Details about SAPAddMessage

I had a similar requirement (mainly for debugging)
My solution was to pop up a small form with a single label control and unload it 4 seconds later. The form proeprties are set no not show modal etc.
in a VBA "Module"
Private mFrmToast As frmToast
Public Sub clearToast()
On Error Resume Next
If Not mFrmToast Is Nothing Then
mFrmToast.Hide
Unload mFrmToast
Set mFrmToast = Nothing
End If
End Sub
Public Sub showToast(message As String)
On Error GoTo er_clear_in_4
If mFrmToast Is Nothing Then
Set mFrmToast = New frmToast
End If
mFrmToast.message = message
If Not mFrmToast.Visible Then
Call mFrmToast.Show(False)
End If
er_clear_in_4:
Application.OnTime Now + TimeValue("00:00:04"), "clearToast"
End Sub
The form "code behind" module contained a sample write only property, "message".
Option Explicit
Public Property Let message(ByVal sMessage As String)
lblMessage.Caption = sMessage
End Property
The usage is simply
showToast("your message here")

Related

How to Know an Alert Pop-Up Has Occurred

I am automating a website. I am using SeleniumBasic in Excel VBA.
I have a simple Pop-Up, or Alert, with an OK button.
I cannot access the Pop_Up by using Inspect so I have no HTML to show.
When the Pop-Up occurs, I respond with Driver.SwitchToAlert.Accept. It goes away, as desired.
I don't know how to check programmatically if the Pop-Up is present.
If I assume that the Pop-Up may have occurred and use this code on a just-in-case basis, i.e. use the code without the Pop-Up existing, the program stops without any error message.
Is there a way to check if the Pop-Up window exists before I respond?
Well, I found the answer:
I got this code from someone who got it from someone else.
Private Function AlertIsPresent() As Boolean
'Returns true if an alert is present, false otherwise
Dim T As String
On Error Resume Next
T = Driver.Title 'This raises error 26 if an alert(pop-up) is displayed
AlertIsPresent = (26 = Err.Number)
End Function
If function returns 'true', use the Driver.SwitchToAlert.Accept to close it.

Email displays but does not send

I am testing out sending emails from Excel using VBA.
I do not get an email sent to my inbox.
The code is below:
Sub email_from_excel()
Dim emailApplication As Object
Dim emailItem As Object
Set emailApplication = CreateObject("Outlook.Application")
Set emailItem = emailApplication.CreateItem(0)
'Now build the email
emailItem.to = "emailaddress#test.com"
emailItem.Subject = "This is a test y'all."
emailItem.Body = "This is a test message ya'll."
'Send the email
emailItem.Send
End Sub
In line 6 of the code, I put my email address in between the quotation marks.
I enabled Microsoft Outlook under Tools - References in VBA. No error comes up, but no email gets sent.
I commented out the emailItem.Send and added in emailItem.Display ran the code and an email showed.
Keep in mind that message submission is an asynchronous process. You need to wait for the message to go out before releasing (and thus closing) Outlook.
As a test, start Outlook before your code runs - this way it will stay open even after your code is finished executing.
I've been through the same problem, what seems to be happening is the Excel deletes outlook from memory before sending the message,
Before the solution here comes a tip, instead of using create object you can declare the variable as application.outlook and application.mailitem
It makes all the methods and propertys appears as hint while you type
Well, I solved it by declaring a global variable to store outlook.application
Global appOutlook as application.outlook
Then in "this workbook" inside the open event I set the variable
Set appOutlook = new application.outlook
Now within the macro that send the e-mail you declare an set the variable to store de email
Dim eMail as Outlook.mailitem
Set eMail = appOutlook.CreateItem(olMailitem)
Also you can improve your code by using with statement
With eMail
.to
.body
End with
Instead of type the car name every time u can use with and then you just need to type a dot and access the methods and propertys
Now the last part is to set eMail to nothing
Set eMail = nothing
I don't know why this is necessary, but my codes usually don't send the e-mail without it
Well this should make it works, you can use a local variable, but if you need to send more than one email in a short time, then any one you go, they will be in some kind of "void" and you go when you run a code that actually works, by using a global variable to store the application, it starts and only stops if you code it, the application crashes or closes, well sorry for the big text, and i hope to help

Sticky MsgBox - doesn't close

The Message Box called here is not closing upon clicking "OK". Keeps popping up until I kill the task via Task Manager.
The runtime environment is WinCE 6.0.
Private Sub frmPAConsole_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
strConfigFileName = "\Program Files\Alarm System\PASystem_AudioFilesAllocation.txt"
If Not IO.File.Exists(strConfigFileName) Then
MsgBox("Configuration file doesn't exist: \Program Files\Alarm System\PASystem_AudioFilesAllocation.txt")
Me.Close()
End If
Call LabelStopButton()
Call ReadConfigFile(strConfigFileName)
Call PopulateButtonsDescription()
End Sub
I tested a message box when just clicking a button - no problem. Also, the device Windows messages are closing normally.
Any ideas?
Thanks!
This seems like some sort of a bug.
Even when implementing the Error Message using a new dialog form - the stickiness of the message persisted ONLY for the IO.file.open error (regardless of whether it was raised by error handling or checked if file.exist).
The only workaround I managed to do is instead of having error messages pop up in new windows, I implemented a textbox on the main form itself, on the bottom, for error messages, and I control its text and visibility.
Thank you all for reading and thinking about this issue.

CreateObject randomly throws "A system shutdown has already been scheduled" error

I googled and SO'd, and nothing.
My job revolves around making my co-workers lives easier.
Currently, they are using very clunky spreadsheets designed 10+ years ago.
In the process of migrating their tools and reports to the local intranet using PHP, i have configured a spreadsheet that downloads that persons permissions based on their Application.Username
Then a little back and forth with the server to generate a session key, and then pop internet explorer opens up with the relevant tool they selected from a dropdown within the workbook - meaning their session and tools are then purely browser based.
All works great, however randomly, sometimes, when the sub to open the internet browser is triggered a very bizarre error message appears :-
Upon clicking Debug, the following function is shown, and you can see for yourself which line is highlighted in yellow.
I can confirm i do not have any tasks at all within my taskschedule. When i end this, and run it again, chances are it runs just fine.. it is just sometimes that this error pops up.
Please help! Thank in advance.
With errors this seemingly-unrelated and intermittent, I usually opt for either a bit of delay, catching the error and retrying or both.
Try the following (retry without a delay):
Function gogogo(sessKey)
On Error GoTo ErrHandler
reportId = Sheet2.Range("A" & (Sheet2.Range("B1").Value + 1)).Value
Set objIE = CreateObject("InternetExplorer.Application")
URL = "http://localinternetdomainhere/OnlineTools/" & reportId & "/access/" & sessKey
With objIE
.Visible = True
.navigate URL
End With
ThisWorkbook.Saved = True
ThisWorkbook.Close False
Exit Function
ErrHandler:
If Err.Number = &H800704A6 Then 'Put a breakpoint here to make sure this is the ACTUAL VBA error number and not the ActiveX one. You might need to check against the Err.LastDllError property
Resume
End If
Err.Raise Err.Number, Err.Source, Err.Description,err.HelpFile, err.HelpContext 'Reraise the error otherwise
End Function

Run resharper silent cleanup on save fails

I'm trying to clean up my code on save using Resharper's Cleanup Code function. I made a macro that handles DocumentSaved events. The important parts:
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
...
DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
document.Save()
End Sub
Every time I save, I get an exception message that says Error HRESULT E_FAIL has been returned from a call to a COM component. Any ideas?
Note: I see How can I configure ReSharper's code cleanup on save? and it won't work in my situation because I need to respond to the save event. Mapping a macro to CTRL+S isn't enough.
I think this is because when you call document.Save() it is recursing and saving again and then it falls over. Try this:
Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
Static currentDocument As EnvDTE.Document
If Not currentDocument Is Document Then
currentDocument = Document
DTE.Windows.Item(Document.Name).Activate()
DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
DTE.ActiveDocument.Save()
End If
End Sub
This worked for me

Resources