MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Play music when your worbook opens

Posted by Jim on December 22, 2001 4:41 PM

Hi group,
Has anyone written code that on Workbook_Open a particular
song would play for a given amount of time say 20 seconds
I think it would be fun, any ideas?"

Posted by Ivan F Moala on December 22, 2001 8:31 PM

Try this change as required;

In ThisWorkbook object

Private Sub Workbook_Open()
WAVPlay "C:\WINDOWS\MEDIA\BasketBall\charge.wav"
End Sub

Change the Full path name of file you want.
You could embed the file into the worksheet
and activate it via the objects verb eg

Sheet2.Shapes("Object 1").Select
Selection.Verb Verb:=xlPrimary

In a Module place this Function;

Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10

Dim SoundName As String
Dim wFlags As Double
Dim X

Sub WAVPlay(File)
Dim SoundName As String
SoundName = File
X = sndPlaySound(SoundName, wFlags)
End Sub