Word

Play sound file with VBA

Ease of Use

Easy

Version tested with

2000, 2003 

Submitted by:

MOS MASTER

Description:

Programmatically play sound file. (*wav) 

Discussion:

You may need a way to give feedback or direction to users but you don?t want to use pop-ups! Well you can use sound feedback instead. 

Code:

instructions for use

			

Option Explicit 'API Class to take care of playing the file Public Declare Function sndPlaySound32 Lib "winmm.dll" _ Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _ ByVal uFlags As Long) As Long Sub VBASound() 'Call Api to play LoadIt.wav which is in the same folder as 'the active document! Call sndPlaySound32(ActiveDocument.Path & "\LoadIt.WAV", 0) End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
  7. Make sure the sound file is in the same folder as the document! (Or change path in code)
 

Test the code:

  1. From Word, press Alt + F8 to open the macro dialog box.
  2. Select VBASound
  3. Click Run.
 

Sample File:

Play Sound.zip 54.75KB 

Approved by mdmackillop


This entry has been viewed 106 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express