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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dirk HaestProject managerCommented:
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
Helen FeddemaCommented:
Here is the code:
Private Sub UserForm_Initialize()

   Beep
   
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Fi69Author Commented:
Many thanks!
0
Dirk HaestProject managerCommented:
>> 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
Dirk HaestProject managerCommented:
>> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.