Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to play wav file

Status
Not open for further replies.

ramnewbietoaccess

Programmer
Nov 4, 2002
52
US
I am trying to in Excel 2000 using vb 6 on event for a command button play a wav file. I have tried two different codes that were on the net. My problem is this....they use declaration statments and i keep getting a debug error message saying that you cannot use declaration statements. Below is the most current attempt but it still debugs at the public static statement. Any suggestions or is there a better code or way to do this?

Thank you in advance.


using System;
using System.Runtime.InteropServices;
using System.Resources;
using System.IO;
namespace Win32
{
public class Winmm
{
public const UInt32 SND_ASYNC = 1;
public const UInt32 SND_MEMORY = 4;
// these 2 overloads we dont need ...
// [DllImport("Winmm.dll")]
// public static extern bool PlaySound(IntPtr rsc, IntPtr hMod, UInt32 dwFlags);
// [DllImport("Winmm.dll")]
// public static extern bool PlaySound(string Sound, IntPtr hMod, UInt32 dwFlags);
// this is the overload we want to play embedded resource...
[DllImport("Winmm.dll")]
public static extern bool PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags);
public Winmm()
{
}
public static void PlayWavResource(string wav)
{
// get the namespace
string strNameSpace=
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
// get the resource into a stream
Stream str =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( strNameSpace +"."+ wav );
if ( str == null )
return;
// bring stream into a byte array
byte[] bStr = new Byte[str.Length];
str.Read(bStr, 0, (int)str.Length);
// play the resource
PlaySound(bStr, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
}
}
}

 
I'm sure I replied to this question the other day but can't find it now. Anyway, a quick google comes up with:
Code:
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long


Sub PlayWavFile(WavFileName As String, Wait As Boolean)
    If Dir(WavFileName) = "" Then Exit Sub ' no file to play
    If Wait Then ' play sound before running any more code
        sndPlaySound WavFileName, 0
    Else ' play sound while code is running
        sndPlaySound WavFileName, 1
    End If
End Sub


Sub TestPlayWavFile()
    PlayWavFile "c:\foldername\soundfilename.wav", False
    MsgBox "This is visible while the sound is playing..."
    PlayWavFile "c:\foldername\soundfilename.wav", True
    MsgBox "This is visible after the sound is finished playing..."
End Sub

you should be fine declaring functions wherever....


Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
thanks i tried that exact code but changed the .dll

I get the error message that says this upon opening the applic ation:

Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules

any ideas?
 
maybe i need to clarify that i am trying to do this with a command button on a user form?
 
The code posted by xlbo must be in a standard code module, mot a form/report module.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
In a form/code module you can call the procedures defined in a standard code module.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Man you guys are the greatest! That is exactly what I needed. Thanks for your time and patience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top