Consulting

Results 1 to 5 of 5

Thread: Solved: Play .wav sound file when userform opened

  1. #1
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location

    Solved: Play .wav sound file when userform opened

    Hello

    I want to be able to play a .wav sound file when a userform is opened. The following code works ok when you enter your own file path etc. I have entered the code in a module and all works ok manually but I can't get it to work automatically when the userform opens.

    [vba]
    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    Private Const SND_FILENAME As Long = &H20000



    Private Sub Knock_Click()
    PlaySound "C:\Knock.wav", 0&, SND_FILENAME
    '/// where "C:\Knock.wav" gets replaced by your wav's path / name.
    End Sub
    [/vba]
    All suggestions gratefully received.
    Gil

  2. #2
    Try.
    [vba]Private Sub UserForm_Activate()
    Beep
    PlaySound "C:\Knock.wav", 0&, SND_FILENAME
    End Sub
    [/vba]

  3. #3
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Hello omnibuster
    Your suggestion worked a treat.This is what I added to suit my project.I inserted it in Microsoft Excel Objects/ThisWorkbook

    [vba]
    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    Private Const SND_FILENAME As Long = &H20000


    Private Sub Workbook_Open()


    PlaySound "C:\Knock.wav", 0&, SND_FILENAME
    '/// where "C:\Knock.wav" gets replaced by your wav's path / name.

    Application.WindowState = xlMaximized
    UserForm1.Show

    End Sub
    [/vba]
    Maybe not the most challenging solution but for a novice like me a great deal of help. If anyone can tell me what the 0&, SND_FILENAME means it may help in my understanding.

    Many thanks for all the help

    Gil

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Gil,

    See if this helps: http://allapi.mentalis.org/apilist/PlaySound.shtml

    Mark

  5. #5
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Hello GTO

    Thank you for the link. Not sure I understand it yet but will be added to my reference library

    Gil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •