Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Can you play sound via VBA?

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks guys...
    Tom

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Posts
    81
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Using VB MIcrosofr has a MCI control (Media control interface)

    this control connects to most spound sources.... guess you could use its properties to play a sound??

    Ed

  3. #3
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Tom
    You can Play virtually anything you like
    and in a number of ways.
    What excatly do you want to do ??

    If you like I can send you example files
    to Play a Wave,Midi,MP3,AVI files located
    either on your drive or embeded in an excel
    File.....
    Kind Regards,
    Ivan F Moala From the City of Sails

  4. #4
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks for the replies...
    I created a spreadsheet for my daughter to practice her multiplication.
    It is set up like a game with command buttons and a timer.
    I'd like to add sounds for various events.

    Wrong Answer
    Right Answer
    High Score
    Timer Clicks
    Ect...

    I could find nothing in help...

    Thanks again!
    Tom

    P.S.
    Thanks Ivan for the offer...
    I'd love to have the example files...
    If you have any kids, I'll send you the spreadsheet when I'm done...

    Tom

    [ This Message was edited by: TsTom on 2002-05-01 01:41 ]

  5. #5
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Tom
    Just to get you started.
    Why not record what you want eg...WRONG ANSWER, RIGHT ON etc....probably sound
    better coming from Mum
    Then just use this code to play the wave files you have recorded.


    Option Explicit

    Public 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 SndFile As String
    Dim wFlags As Double
    Dim PlayIt

    Sub WAVPlay(File)
    Dim SoundName As String

    SoundName = File

    wFlags = SND_ASYNC Or SND_NODEFAULT

    PlayIt = sndPlaySound(SoundName, wFlags)

    End Sub

    Sub Wrong()
    SndFile = "C:MEDIAwrong.wav"

    If Dir(SndFile) = "" Then MsgBox "Sorry no File to Play!": End

    PlayIt = sndPlaySound(SndFile, SND_ASYNC)

    End Sub




    Tom....I have Kids would appreciate a copy


    Kind Regards,
    Ivan F Moala From the City of Sails

  6. #6
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    What you have right there is great, Ivan...
    Would like to know how to play midi files as well.
    Thanks!

  7. #7
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-05-01 02:32, TsTom wrote:
    What you have right there is great, Ivan...
    Would like to know how to play midi files as well.
    Thanks!

    Option Explicit


    Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" _
    (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, _
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    '// Function Discription:
    '// The mciSendString function sends a command string to an MCI device.
    '// The device that the command is sent to is specified in the command string.
    '//
    '// Success:
    '// Returns zero if successful or an error.
    '// The low-order word of the returned doubleword value contains the error return value.
    '// If the error is device-specific, the high-order word of the return value is the
    '// driver identifier; otherwise, the high-order word is zero.
    '//
    '// Parametre discriptions:
    '// -lpszCommand
    '// Address of a null-terminated string that specifies an MCI command string.
    '// For more information about the command strings, see Command Strings.
    '//
    '// -lpszReturnString
    '// Address of a buffer that receives return information.
    '// If no return information is needed, this parameter can be NULL.
    '//
    '// -cchReturn
    '// Size, in characters, of the return buffer specified by the lpszReturnString parameter.
    '//
    '// -hwndCallback
    '// Handle of a callback window if the "notify" flag was specified in the command string.


    Const sMidiFile As String = "C:WINDOWSMEDIAimposs[1].mid "

    Dim Play

    Sub Play_Midi()
    '// Discription:
    '// Plays a Midi sound File
    '// Specified by Const sMidiFile
    '//

    Play = mciSendString("play " & sMidiFile, 0&, 0, 0)
    If Play <> 0 Then MsgBox "Can't PLAY!"

    End Sub

    Sub Stop_Midi()
    Play = mciSendString("close " & sMidiFile, 0&, 0, 0)
    End Sub






    Kind Regards,
    Ivan F Moala From the City of Sails

  8. #8
    Board Regular
    Join Date
    Mar 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    6,824
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks alot guys and Ivan especially, as usual.
    Tom

  9. #9
    Board Regular
    Join Date
    Apr 2002
    Location
    A Mile High!!
    Posts
    241
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Tom --

    I would like a copy too if that is okay when youre done. My kid will do these things as long as they are "cool."

    thanks!!

  10. #10
    Board Regular
    Join Date
    Apr 2002
    Posts
    135
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I would like a copy when its done, Thanks
    Dsuperc@hotmail.com

Some videos you may like

User Tag List

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
  •