+ Reply to Thread
Results 1 to 6 of 6

VBA to make Excel sound noise when done with countdown

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2019
    Location
    New Hampshire
    MS-Off Ver
    Excel 2011
    Posts
    9

    VBA to make Excel sound noise when done with countdown

    With help from a few nice members I was able to get my countdown timer to work well. The only thing I would like to do it to add a bell/buzzer for 10 seconds when a countdown is complete. I can get a simple ding, but not a real bell/buzzer type alert. I am running on a mac if that matters.

    Toughts?
    -Frank
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: VBA to make Excel sound noise when done with countdown

    .
    This is one method ... certain there are plenty more :

    Option Explicit
    
    
    #If VBA7 Then
        Declare PtrSafe Function PlaySoundAPI Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
        Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
        Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
    #Else
        Declare Function PlaySoundAPI Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
        Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
        Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    #End If
    
    Const SND_LOOP = &H8
    Const SND_ASYNC = &H1
    Const SND_FILENAME = &H20000
    
    Const WAV_FILE_PATH_NAME As String = "C:\Windows\Media\Chimes.wav"  ' <== Change WAV file as required here.
    Const SNOOZE_INTERVAL As Long = 10 'Secs  ' <== Change snooze duration as required here.
    
    Sub StartAlarm()
        Application.OnKey " ", "MuteAlarm"
        PlaySoundAPI WAV_FILE_PATH_NAME, ByVal 0&, SND_FILENAME Or SND_ASYNC Or SND_LOOP
    End Sub
    
    Sub StopAlarm()
        Call MuteAlarm(Snooze:=False)
    End Sub
    
    Sub MuteAlarm(Optional ByVal Snooze As Boolean = True)
        KillTimer Application.hwnd, 0
        PlaySoundAPI vbNullString, 0, 0
        Application.OnKey " "
        If Snooze Then
            Application.OnKey " ", "MuteAlarm"
            SetTimer Application.hwnd, 0, SNOOZE_INTERVAL * 1000, AddressOf StartAlarm
        End If
    End Sub

    In your code where you have this line : If Range("d3").Value = 0 Then Exit Sub

    Change to :

    If Range("d3").Value = 0 Then
    'Call Play Sound macro here
    Exit Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    02-21-2019
    Location
    New Hampshire
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: VBA to make Excel sound noise when done with countdown

    So in this case the .wav needs to be on the computer used. Is there a way to imbed that in the workbook so others can use it?

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation


    To Logit : you post a Windows code but the OP wrote he uses a MAC …

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: VBA to make Excel sound noise when done with countdown

    .
    Apologies ... overlooked he was running a MAC.

    If by chance the code works on your MAC, the easiest thing would be to use a .WAV file already in Windows.

    I am not familiar with MACs ... so ... if by chance MAC doesn't use .was files ... again I apologize.


  6. #6
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,313

    Re: VBA to make Excel sound noise when done with countdown

    The link I refereed to in your previous post was developed for Mac.
    Within the very comprehensive dialog there must be a guide as to the addition of sound.
    I have briefly run your app and it suffers from similar problems that befell the original in the link refered to.
    Yours is referring back to a common 'interval' in seven cases and gets out of 'sync' (speeds up/slows down/jumps/misses periods).
    This was solved in the attached examples by assigning each timer their individual 'tick' constant.


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Countdown timer and playing sound
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-18-2019, 01:50 AM
  2. [SOLVED] how to make a timer ( stopwatch ) or countdown in Ms.Excel using macros
    By dolphino in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2015, 10:49 PM
  3. How to make a countdown timer on Excel?
    By benyam in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 07-30-2012, 07:17 PM
  4. How do you make a countdown clock in excel?
    By janetdanielkotz in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-08-2012, 09:44 AM
  5. [SOLVED] How to make a cell trigger a Sound in Excel
    By justice in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-11-2006, 01:45 PM
  6. [SOLVED] How do I make a sound as a result of an "if" statement in excel
    By george in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-01-2005, 07:06 PM
  7. PLEASE HELP - Annoying beep noise when I close Excel
    By korinyoshida in forum Excel General
    Replies: 0
    Last Post: 02-21-2005, 04:49 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1