PDA

View Full Version : [SOLVED] How Do You Make the "Beep" API Work?



Cyberdude
04-20-2005, 02:32 PM
Every so often I try to get the "Beep" program to work for me, and all I ever get is silence. It may be an audio system problem. Here's my latest effort:


Declare Sub MessageBeep Lib "User32" (ByVal N As Long)

Sub Beep()
Call MessageBeep(0)
End Sub

Has anyone been able to make it work?

Jacob Hilderbrand
04-20-2005, 02:45 PM
I haven't tried that one, but VBA has a built in Beep. Just put Beep on a line.



Sub Macro1
Beep
End Sub

TheAntiGates
04-20-2005, 03:55 PM
First change 0 to &H0 - the argument is Long. And Yikes! Declare Sub instead of Declare Function! Make that change, and append "As Boolean" to the function declaration, as is shown in the function reference.

As DRJ might be implying, the safer course is to just use native VBA functions. There's far more protection with those. But with API/DLLs you need to be precise; these are more like coding in the c language and you can hurt yourself if you misuse them. Make sure you're on top of the concepts in "Office VBA and the Windows API": http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/Office03082001.asp

Function references are also critical when working with API: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/messagebeep.asp

An arg summary:
uType
If set to -1 (or &HFFFFFFFF), plays a beep using the computer's internal speaker. Otherwise, this is exactly one of the following flags specifying which sound to play:

MB_ICONASTERISK = &H40 ' Play the SystemAsterisk sound.
MB_ICONEXCLAMATION = &H30 ' Play the SystemExclamation sound.
MB_ICONHAND = &H10 ' Play the SystemHand sound.
MB_ICONQUESTION = &H20 ' Play the SystemQuestion sound.
MB_OK = &H0 ' Play the SystemDefault sound.
(tags used simply to preserve whitespace)

Cyberdude
04-20-2005, 05:03 PM
I'm not hung up on the API approach. I just didn't realize that VBA had one too. OK, so I tried the VBA with


Sub BeepMe()
Dim N%
For N = 1 To 3
Beep
Next N
End Sub

What I got was an error message saying the the stack limit had been exceeded. So I got rid of the loop, and executed just Beep, and got the same message. It's not as though it's a complicated procedure ... so how did the stack overflow, and why are they even using a stack??

Jacob Hilderbrand
04-20-2005, 05:18 PM
That code looks fine to me. Not sure what the error could be. There could be a problem with your Office installation. Try a reboot then open a new workbook and try the macro again to see if the problem goes away.