+ Reply to Thread
Results 1 to 10 of 10

How to use a keyboard shortcut to play a specified audio file within a Macro

  1. #1
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Question How to use a keyboard shortcut to play a specified audio file within a Macro

    Please could someone kindly help with the macro code to play a sound in Excel without it leaving the sheet. I have tried so many options but none have worked. From what I've read, I understand that the best way is to use Windows Media Player.

    Ideally I would like to use a keyboard shortcut say Ctrl+Shift+L and it play a specific audio file as specified within the macro. I'm just starting to learn Macros and VBA, please be gentle with me.

    many thanks
    Richard
    Last edited by Richard Motivator; 10-11-2020 at 06:18 PM.

  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,021

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    .
    Paste the following into a REGULAR module :

    Please Login or Register  to view this content.
    You can play the TADA sound file by pasting a COMMAND BUTTON on a sheet and attach it to the DPlaySound macro.


    This resource can assist you in assigning key combinations to run the sound macro : https://www.rondebruin.nl/win/s4/win012.htm
    There are other websites out there that cover the same type information. Google: run macro key combination

  3. #3
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Thank you for your help. I have pasted that code but get a compilation error:

    Only comments may appear after End Sub, End Function, or End Property

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

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    .

    Option Explicit

    Private Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

    Sub DPlaySound()
    Dim PlaySound As Boolean
    Call sndPlaySound32("C:\windows\media\tada.wav", 1)
    End Sub
    Please Login or Register  to view this content.
    Paste the above, exactly as is, in a REGULAR MODULE. Do not have anything else above it or below it.

    If necessary, place this code in a brand new module by itself.

  5. #5
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Thank you very much, I now have it working!!

    Could I kindly ask how I create another shortcut to stop it playing (in the case of a longer track)

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

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    .
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Thank you for that, I can get them to work 'stand alone'. What I would like to do now, is to call the sound from one of my other subroutines. As you know just pasting it in causes compilation errors. I have also tried using the same shortcut letter as the other routine, but only the 'sound' one runs if I do this. Please could you help again

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

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    .
    Let's use this as a sample :

    Please Login or Register  to view this content.

    In another macro you want to run the wave file shown above, it would look like this :

    Please Login or Register  to view this content.
    Keep in mind that FOO and MsgBox "Testing" are simply examples of code that might be found in another macro.
    The important thing to know is ... to run the macro that plays the sound file, insert DPlaySound in the macro when you
    want to play the sound.

  9. #9
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Thank you, not sure exactly 'what's going on' but it works. I'm wondering if you'd mind showing me how to do something similar, but this time to play a video residing on my PC like the sound file. I'd like the video to play, but when finished to automatically come back to the worksheet. Do I need to 'embed' the video to do this, to avoid seeing the media player, or can it seamlessly play without that need?

    many thanks
    Richard

  10. #10
    Registered User
    Join Date
    10-11-2020
    Location
    Stoke
    MS-Off Ver
    2019
    Posts
    11

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Re: How to use a keyboard shortcut to play a specified audio file within a Macro

    Thank you, not sure exactly 'what's going on' but it works. I'm wondering if you'd mind showing me how to do something similar, but this time to play a video residing on my PC like the sound file. I'd like the video to play, but when finished to automatically come back to the worksheet. Do I need to 'embed' the video to do this, to avoid seeing the media player, or can it seamlessly play without that need?

    many thanks
    Richard

+ 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. Replies: 1
    Last Post: 03-23-2015, 12:25 AM
  2. [SOLVED] Workbook_Open() doesn't run when file is called via keyboard shortcut
    By batman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2013, 08:55 PM
  3. [SOLVED] Macro launched with keyboard shortcut breaks after a "File Open" action
    By Skotzmun in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-04-2013, 03:46 AM
  4. Play audio when an excel cell's condition is true.
    By gorge004 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-05-2013, 11:01 AM
  5. [SOLVED] How to make audio play when you click image rather than a cell?
    By Cludo in forum Excel General
    Replies: 6
    Last Post: 06-04-2012, 05:36 AM
  6. Play Audio File
    By smweasel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2011, 05:47 PM
  7. Keyboard Shortcut in Macro
    By osaka78 in forum Excel General
    Replies: 3
    Last Post: 01-16-2006, 04:50 AM

Tags for this Thread

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