Consulting

Results 1 to 5 of 5

Thread: How Do You Make the "Beep" API Work?

  1. #1

    How Do You Make the "Beep" API Work?

    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?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    I haven't tried that one, but VBA has a built in Beep. Just put Beep on a line.


    Sub Macro1
    Beep
    End Sub

  3. #3
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    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/de...ce03082001.asp

    Function references are also critical when working with API: http://msdn.microsoft.com/library/de...essagebeep.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)
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  4. #4
    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??

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •