+ Reply to Thread
Results 1 to 19 of 19

How to disable and enable sound wave files in Excel using VBA

Hybrid View

KSChan How to disable and enable... 09-13-2022, 09:52 AM
ByteMarks Re: How to disable and enable... 09-14-2022, 05:14 PM
KSChan Re: How to disable and enable... 09-14-2022, 10:23 PM
ByteMarks Re: How to disable and enable... 09-15-2022, 07:09 AM
KSChan Re: How to disable and enable... 09-15-2022, 08:16 AM
ByteMarks Re: How to disable and enable... 09-15-2022, 08:18 AM
KSChan Re: How to disable and enable... 09-15-2022, 10:27 AM
Marc L Re: How to disable and enable... 09-15-2022, 10:33 AM
KSChan Re: How to disable and enable... 09-16-2022, 04:06 AM
Marc L Re: How to disable and enable... 09-16-2022, 04:35 AM
KSChan Re: How to disable and enable... 09-16-2022, 09:25 AM
Marc L Re: How to disable and enable... 09-16-2022, 09:43 AM
Marc L Re: How to disable and enable... 09-16-2022, 10:01 AM
KSChan Re: How to disable and enable... 09-17-2022, 02:12 AM
Marc L Re: How to disable and enable... 09-17-2022, 08:39 AM
KSChan Re: How to disable and enable... 09-17-2022, 11:38 PM
Marc L Re: How to disable and enable... 09-18-2022, 07:31 AM
KSChan Re: How to disable and enable... 09-18-2022, 08:02 AM
Marc L Re: How to disable and enable... 09-18-2022, 08:07 AM
  1. #1
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    How to disable and enable sound wave files in Excel using VBA

    Hi, this is still about disabling and enabling the sound files.

    Using Forum Contributor, Squeaky's idea, I came up with some VBA codes.
    The idea is this:

    I'm using a toggle button to simulate a "Turn Sound ON/OFF" button.
    Press once, a cell will display the word TRUE and if press a second time,
    that cell will display the word FALSE.

    This cell that displays the TRUE & FALSE word is linked to a module which contains
    the coding for playing the sound wave files.
    If the cell displays FALSE, the sound file will not be played, else (if TRUE)
    the sound will play.

    To test if the disabling and enabling the sound works, I've a cell where the
    user types in the letter A or B and click the "Answer" button (to run the macro).
    Typing either A or B will cause a message box to display, accompanied by a sound.

    So if by toggling the "Turn Sound ON/OFF" button, one should expect to hear the sound
    or hear no sound.

    However, the coding ran into a problem:
    "Compile error: User-defined type not defined" in Module 2 on "Dim BM As ActiveWorkSheets".

    I've attached a sample of the Excel file so that what I mentioned above made sense.
    .Module 2 contains the coding for playing the sound files from C:\Windows\Media.
    .Module 1 contains the coding for A & B message boxes & "Call Playwav".
    .Codes in Sheet1 is for the toggle button.

    I hope someone can help solve that error or improve on the coding to make it
    more efficient.

    Thank you.
    ChanKS
    Attached Files Attached Files

  2. #2
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,086

    Re: How to disable and enable sound wave files in Excel using VBA

    I don't think ActiveWorkSheets exists.

    Maybe something like

    Dim BM As Worksheet
    Set BM = ActiveSheet

  3. #3
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Thank you ByteMarks.
    I made the changes but the Compile error "User-defined type not defined" is still there.

  4. #4
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,086

    Re: How to disable and enable sound wave files in Excel using VBA

    Which line is it this time?

  5. #5
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi ByteMarks.
    It's still the same line in Module 2: Dim BM As ActiveSheet.
    Thanks for looking into it.

  6. #6
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,086

    Re: How to disable and enable sound wave files in Excel using VBA

    You need to declare BM as a WorkSheet (not ActiveSheet) and then Set it to the ActiveSheet

  7. #7
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi ByteMarks, I made the change as you mentioned.

    I changed Dim BM As ActiveSheet to Dim BM As Worksheet, and
    left SET BM = ActiveSheet("Sheet1").Select as it is.

    I got an error: Run-time error '438': Object doesn't support this property or method. The whole line SET BM=ActiveSheet("Sheet1").Select is highlighted as error.

    So I made another change. I changed SET BM=ActiveSheet("Sheet1").Select to SET BM=Worksheet("Sheet1").Select.

    I got a new error message: Compile error: Sub or Function not defined. Only the word Worksheet in SET BM=Worksheet("Sheet1").Select is highlighted as error.

    Thanks for looking into these problems.

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

    Arrow Re: How to disable and enable sound wave files in Excel using VBA


    Hi, as using an object variable is useless here so just use ActiveSheet like you can check in VBA help ...

    And for your last error it's just a typo 'cause you forgot the last 's' of Worksheets !
    Last edited by Marc L; 09-15-2022 at 10:36 AM.

  9. #9
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi Marc L.
    Thank you for your input. I put in the missing 's' but still the same error messages.
    I have many permutations - changing ActiveSheet to Worksheet, put 's' and without 's', leaving out the word "Select", etc. and still ended up with
    either "Sub or Function not defined' or 'Object doesn't support this property or method'.

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

    Arrow Re: How to disable and enable sound wave files in Excel using VBA


    Rather than a guessing challenge post your actual code using the # icon, in order some helper could see the light ...

  11. #11
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi to all experts. Hoping some people can help to find what's wrong with the coding.
    This code is in Module 2.

    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszSoundName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    Const SND_SYNC = &H0
    Const SND_ASYNC = &H1
    Const SND_FILENAME = &H20000
    ---------------------------------------------------------------------------------
    
    Sub Playwav()
    'Play this sound when the answer is wrong (B).
    Dim BM As Worksheets
    SET BM = ActiveSheet("Sheet1")
    If BM.Range("P4") = "OFF" Then
    
    Exit Sub
    Else
        
        If Application.CanPlaySounds Then
        Call PlaySound("C:\Windows\Media\Windows Error", 0&, SND_ASYNC Or SND_FILENAME)
        End If
    End If
    End Sub
    
    -----------------------------------------------------------------------
    
    Sub Playwav2()
    'Play this sound when the answer is correct (A).
    Dim BM As Worksheets
    Set BM = ActiveSheet("Sheet1")
    If BM.Range("P4") = "OFF" Then
    
    Exit Sub
    Else
        
        If Application.CanPlaySounds Then
        Call PlaySound("C:\Windows\Media\tada", 0&, SND_ASYNC Or SND_FILENAME)
        End If
    End If
    End Sub
    The error "Run-time error '438': Object doesn't support this property or method." is on the line
    SET BM = ActiveSheet("Sheet1").

    Thank you.
    Chan KS

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

    Exclamation Re: How to disable and enable sound wave files in Excel using VBA


    Like explained in post #2, #6 & #8,
    like explained in VBA help - a must read ! - do not use ActiveSheet if you need to specify a sheet name
    but the Sheets or Worksheets collection ...

    And for a unique access better is to directly qualify the sheet and the range without any useless object variable
    then your VBA procedure should need a single codeline !
    Last edited by Marc L; 09-16-2022 at 09:55 AM.

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

    Re: How to disable and enable sound wave files in Excel using VBA


    Or better just using the sheet CodeName when it is located in the same workbook containing the VBA procedure

    so just removing the useless :

    PHP Code: 
    Private Declare PtrSafe Function PlaySoundALib "winmm.dll" (ByVal pszSound$, ByVal hmod&, ByVal fdwSound&)

    Sub PlayWav()
        If 
    Sheet1.[P4Then PlaySoundA "C:\Windows\Media\Windows Error.wav"0&, &H20001
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon ? Add Reputation ? !
    Last edited by Marc L; 09-16-2022 at 10:31 AM.

  14. #14
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi Marc L.
    Changed to your code, but still it's not working.

    Error: Run-time error '13': Type mismatch.
    Highlighted in yellow on these words only: If Sheet1.[P4] Then.

    ChanKS

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

    Arrow Re: How to disable and enable sound wave files in Excel using VBA


    As it well works on my side the bad is on yours ...

  16. #16
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi Marc L,
    I changed "If Sheet1.[P4] Then" to

    "If Sheet1.[P4] = "ON" Then" and it works now! No more "Type Mismatch" error.

    However, with PlaySoundA "C:\Windows\Media\Windows Error.wav", 0&, &H20000, the sound comes first then followed by the message.

    Then I accidentally discovered that by changing the above to:
    PlaySoundA "C:\Windows\Media\Windows Error.wav", 0&, SND_ASYNC Or SND_FILENAME the sound and message came together.

    Thank you very much for your help!
    ChanKS
    Last edited by KSChan; 09-18-2022 at 12:41 AM. Reason: Need to add another comment

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

    Arrow Re: How to disable and enable sound wave files in Excel using VBA


    Quote Originally Posted by KSChan View Post
    However, with PlaySoundA "C:\Windows\Media\Windows Error.wav", 0&, &H20000, the sound comes first then followed by the message.
    Your bad as this is not my original codeline !!

  18. #18
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: How to disable and enable sound wave files in Excel using VBA

    Hi Marc L,
    You're right! I made the mistake by typing your original ...&H20001... as &H20000.
    Now with your original codeline, the sound and message came together!
    Thank you.

    ChanKS.

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

    Arrow Re: How to disable and enable sound wave files in Excel using VBA


    Yes according to Arithmetic basics as you can easily check &h1 (so 1) or &h20000 = &h20001 !

+ 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. How to disable and enable sound wave files in Excel using VBA
    By KSChan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2022, 10:22 PM
  2. Excel 2007 : Disable (Warning) Sound In Excel 2010
    By :) Sixthsense :) in forum Excel General
    Replies: 3
    Last Post: 03-07-2017, 02:22 PM
  3. [SOLVED] Excel Button Enable and Disable
    By excel_student_98 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2013, 05:34 AM
  4. Enable/disable copy paste (disable part not working)
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2012, 07:23 PM
  5. how can i enable or disable the paste option in excel 2000
    By mianmian in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 07:05 AM
  6. [SOLVED] How do I enter a wave sound file into an excel cell?
    By dkimball in forum Excel General
    Replies: 2
    Last Post: 03-28-2005, 04:06 PM
  7. [SOLVED] sound files in excel?
    By dkimball in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 03-24-2005, 09:06 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