How to Mute/Enable Windows Sounds whilst running VBA code

H

Hotbird

Most of the time, my PC runs silently. I am easily irritated by sounds of
windows opening, etc. etc. But exceptionally, I would like to be advised of
an unusual event by playing a Wav file through code. Is there a pair of
lines that I can add to enable before, and mute after the required sound has
played? Here is an example:

Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Public Sub PrinterCheck()

Dim int1as integer
Dim strWavFile As String

int1 = MsgBox("Ready to Print?", vbYesNoCancel)
boolNP = True
If int1 = 6 Then
strWavFile = ThisWorkbook.Path & "\sound1.wav"
boolNP = False
ElseIf int1 = 7 Then
strWavFile = ThisWorkbook.Path & "\sound2.wav"
End If
Call PlaySound(strWavFile, 0&, SND_ASYNC Or SND_FILENAME)

End Sub
 
B

Bob Phillips

Give this a try

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Sub ChangeVolumeMute()
Dim oShell As Object

On Error Resume Next
Set oShell = GetObject(, "WScript.Shell")
If oShell Is Nothing Then
Set oShell = CreateObject("WScript.Shell")
oShell.Activate
End If
oShell.Run "Sndvol32"
Sleep 1500
oShell.SendKeys "{TAB 2} "
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
S

Soo Cheon Jheong

Hi,

Try the following code:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub TEST()

Dim WSS As Object

On Error Resume Next
Set WSS = GetObject(, "WScript.Shell")
If WSS Is Nothing Then Set WSS = CreateObject("WScript.Shell")
On Error GoTo 0

WSS.Run "Sndvol32"
Sleep 1000
WSS.SendKeys "{TAB 2} %PX"

Set WSS = Nothing

End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


--
Regards,
Soo Cheon Jheong
_ _
^¢¯^
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top