In the code below, the song is played first, after which the message is displayed. How to display the Message Box and Sound Play simultaneously in the code below?

Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal 
dwFlags As Long) As Boolean

Private Sub CommandButton1_Click()
   Call PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_FILENAME)
   Msgbox "Process completed successfully."
End Sub
  • The msgbox appears immediately and won't depend on you pressing OK to run the next line. Why not just swap the call playsound and msgbox lines around? – spikey_richie Nov 23 '18 at 8:18
  • 2
    @spikey_richie MsgBox is modally displayed and halts execution until it returns (= the user dismisses it). The sound will only be played once the dialog is closed. – Inarion Nov 23 '18 at 8:21
  • Can you show the assignments for SND_ASYNC and SND_FILENAME as well? (Although I assume the latter is set correctly, otherwise you'd get the wrong sound played.) – Inarion Nov 23 '18 at 8:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.