XL: How to Play Sounds Using Visual Basic for Applications

Last reviewed: February 27, 1998
Article ID: Q158140
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, version 7.0

SUMMARY

In Microsoft Excel 97, cell notes have been replaced with cell comments and no longer support sound notes. However, you can use a Visual Basic for Applications procedure to play sounds. In order to play a sound in a Visual Basic for Applications procedure, a Windows API function, sndPlaySound32(), can be used to play a wave (.wav) file.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

To use the macro, enter the following declaration and sub procedure into a Module sheet. Run the PlaySound macro to hear the chimes wave file.

   'This function declaration must be entered onto a single line.
   Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
       (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

   Sub PlaySound()
       If Application.CanPlaySounds Then
           'Substitute the path and filename of the sound you want to play
           Call sndPlaySound32("c:\win95\media\chimes.wav", 0)
       End If
   End Sub

You can replace C:\Win95\Media\Chimes.wav with any valid wave file.

NOTE: It may be possible to work around this situation by creating a macro that makes calls to the Windows application programming interface (API). This type of programming is supported by the Windows Software Development Kit (SDK) and the Visual Basic, Professional Edition, support groups. The level of support you can receive from these groups depends on the individual support policies of the group. (Microsoft Support Engineers may not be able to assist in specific construction of macros that use API programming.) If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

For additional information on comments in cells, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q156200
   TITLE     : XL97: Sounds Not Converted with Cell Notes

   ARTICLE-ID: Q149777
   TITLE     : XL: How to Play a Warning Sound If Critical Value Is
               Exceeded


Additional query words: XL97 xl7 7.00 8.00 97 api
Keywords : kbcode kbprg xlvbahowto
Version : WINDOWS:7.0,97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.