How to add a sound when a VBA UserForm opens

Hi experts

In VBA, when the default MessageBox appears, eg Msgbox "Hello", there is a little "ding" sound.

I'd like to replicate this sound when my VBA UserForm is initialised.

Any ideas how I do this?
Fi69Asked:
Who is Participating?
 
Helen FeddemaCommented:
Here is the code:
Private Sub UserForm_Initialize()

   Beep
   
End Sub

Open in new window

0
 
DhaestCommented:
You can try the following


' top of code inside a sheet
Option Explicit
Option Base 0
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

' rest of code

' use playsound like this:
PlaySound "C:\windows\media\ding.wav", 0, 0

Open in new window

0
 
Helen FeddemaCommented:
You can do this with one word -- Beep -- from the UserForm Initialize event.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Fi69Author Commented:
Many thanks!
0
 
DhaestCommented:
>> Beep -- from the UserForm Initialize event.

That makes a sound, but not the sound of a messagebox !
0
 
Fi69Author Commented:
Just tested it and confirm that it does sound the same on mine. Different settings perhaps?
0
 
DhaestCommented:
>> Different settings perhaps?

That's true. That's why I thought the asker perhaps needed something else than beep, but anyway, the problem has been solved.
0
 
Fi69Author Commented:
Thanks very much for your help!
0
 
Helen FeddemaCommented:
Beep sounds the same as the message box on my computer, too.  So if there is a difference it is probably because a custom sound was assigned to the message box.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.