Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: Using Vba to run/open an audio file

  1. #1
    New Member
    Join Date
    Nov 2011
    Posts
    20
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Using Vba to run/open an audio file

    A very cold hello as its -16 centigrade here and thought I should start working on my new project.

    I have been searching through vba sites for a simple code input that enables me to have a macro play and audio file.

    Now, I understand it may need to be saved as a wav file but cannot get anything to open.

    Details to consider:
    Macro is activated then, application is opened, file played, application closes

    audio format is .wav (but could be mp3 or wmp)

    I am using WMP to play file (but can change to another)

    So, with that in mind, does anyone know the criteria for the code?
    example code (obviously its not real code, just explanitary code)

    Run macro playfile()
    open.wmp.play."c: blah blah blah .wav"
    close application

    end

    Cheers

  2. #2
    New Member
    Join Date
    Nov 2011
    Posts
    20
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Using Vba to run/open an audio file

    I have found this but has syntax error!/!/
    Private Declare Function PlaySound Lib "winmm.dll"_
    Alias "PlaySoundA" (ByVal lpszName As String, _
    ByVal hModule As Long, ByVal dwFlags As Long) AsLong

    Sub Play_Wav_File()

    Dim sWAVFile As String

    On Error GoTo Err_Play

    sWAVFile = "C:\Temp\Windows XP Logon Sound.wav"

    PlaySound(sWAVFile, &O0, 0)

    Err_Play:
    If Err <> 0 Then
    Err.Clear()
    End If

    End Sub

  3. #3
    MrExcel MVP shg's Avatar
    Join Date
    May 2008
    Location
    The Great State of Texas
    Posts
    21,017
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Using Vba to run/open an audio file

    Try this:

    Code:
    Private Declare Function PlaySound Lib "winmm.dll" _
            Alias "sndPlaySoundA" _
            (ByVal lpszSoundName As String, _
            ByVal uFlags As Long) As Long
     
    Sub Play_Wav_File()
        Dim sWAVFile    As String
     
        sWAVFile = "C:\Temp\Windows XP Logon Sound.wav"
     
        On Error Resume Next
        PlaySound sWAVFile, 0
        If Err.Number Then Err.Clear
    End Sub

  4. #4
    New Member
    Join Date
    Nov 2011
    Posts
    20
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Using Vba to run/open an audio file

    Hey,
    That looks better but has an error:
    Compile error
    Ambiguous name detected:PlaySound
    "Sub Play_Wav_File()" is highlight light blue
    ?!

  5. #5
    MrExcel MVP shg's Avatar
    Join Date
    May 2008
    Location
    The Great State of Texas
    Posts
    21,017
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Using Vba to run/open an audio file

    Delete the declaration you had before.

  6. #6
    Board Regular
    Join Date
    Apr 2011
    Posts
    69
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Using Vba to run/open an audio file

    Here's an Excel MP3 player ready to use:
    bulevardi

    You can use the code and modify for your needs!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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