PDA

View Full Version : Solved: Silence Sound when MsgBox is used?



GregE
04-16-2008, 12:30 PM
Hi All:

This sould be an easy one for all you pros...

How do you get the MsgBox command to display a message without there also being a "Dunk" sound? Is there some way to have these messages displayed quietly or for that matter with an alternate sound?

Greg

fionabolt
04-17-2008, 01:14 AM
Personally I just mute the volume. Another solution would be to tick the check box for "Provide Feedback With Sound" found under Tools, Options, General, if you wanted to tick this this programmatically:

Options.EnableSound = False

GregE
04-17-2008, 07:14 AM
I tried using Options.EnableSound = False as a separate line of code right before the MsgBox line of code, but the sound is still there.


Am I using this command properly?

Greg

fumei
04-22-2008, 05:42 PM
You can't. MsgBox uses API.

You can however, if you want, make your own messagebox - ie. a userform, and make it use your own sound. However, MsgBox itself will use MessageBeep Lib "user32" .

GregE
04-23-2008, 08:33 AM
Thanks Gerry:

I'm not familiar with userforms, so I guess I'll just live with the built in sound.

Greg

Oorang
04-23-2008, 09:30 PM
An interesting question. Fumei is right. MsgBox is basically just a wrapper for the MessageBox API of User32. But the nice thing to know is that the API determine it's sounds off of the sounds set in "Sounds And Audio" in the control panel. And those settings are store in the registry. So you could change the registry back and forth when message boxing.

Note1: A nice side effect is that you could set the entry to a .wav of your own instead of blank ;)
Note2: The vbCritical message box and vbExclamation use different keys to determine sound. Shouldn't be to hard to figure out though :)
Note3: After a little more testing, this code seems to work, but I would break test it hard. I still don't have full confidence in it. So use at your own risk.


Option Explicit

Public Sub TestSilentBox()
'Written by Aaron Bush 4/24/2008
'Provided with no restriction as-is with no warranty express or implied.
'Tested only on Windows XP, not fully tested.
Const strScheme_c As String = "HKEY_CURRENT_USER\AppEvents\Schemes\"
Const strKeyDefaultSound_c As String = "HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\.Default\.Current\"
Dim wshl As Object
Dim orgVal As String
Dim orgScheme As String
Dim blnSafetyCheck As Boolean
On Error GoTo Err_Hnd
'Create Windows Script Host Shell Object:
Set wshl = CreateObject("WScript.Shell")
'Get value of registry key that indicates which sound scheme is in use:
orgScheme = wshl.RegRead(strScheme_c)
'Get value of registry key that indicates which sound is being used for default beep:
orgVal = wshl.RegRead(strKeyDefaultSound_c)
'Change the sound for default beep in in the "Current Scheme" to an empty
'string (another words no sound):
'(Current could also be thought of as "Custom".)
wshl.RegWrite strKeyDefaultSound_c, vbNullString, "REG_EXPAND_SZ"
'Change the key that indicates the sound scheme in use is Current/Custom:
wshl.RegWrite strScheme_c, ".current", "REG_SZ"
'Now that the default beep is turned off, use the message box:
MsgBox "Silent"
'Restore the original sound settings:
wshl.RegWrite strKeyDefaultSound_c, orgVal, "REG_EXPAND_SZ"
wshl.RegWrite strScheme_c, orgScheme, "REG_SZ"
'This is used to indicate the sound settings were restored:
blnSafetyCheck = True
'Give an example using the restored sound settings:
MsgBox "With Sound"
Exit_Proc:
On Error Resume Next
'Verify sound settings were restored and if not, attempt to restore them.
If Not blnSafetyCheck Then
If LenB(orgVal) Then 'Only try if you know the original value.
wshl.RegWrite strKeyDefaultSound_c, orgVal, "REG_EXPAND_SZ"
End If
If LenB(orgScheme) Then 'Only try if you know the original value.
wshl.RegWrite strScheme_c, orgScheme, "REG_SZ"
End If
End If
'Release Windows Script Host Shell Object:
Set wshl = Nothing
Exit Sub
'Handle Errors:
Err_Hnd:
MsgBox Err.Description, vbSystemModal, "Error: " & Err.Number
Resume Exit_Proc
End Sub

GregE
04-24-2008, 09:26 AM
Thanks Aaron:

That code seems to work, now all I have to do is figure out just what you did!!

I'm pretty green at this, so my code vocabulary is pretty limited at this point.


Thanks again... Greg

Oorang
04-24-2008, 10:34 AM
now all I have to do is figure out just what you did
I edited my post to include comments explaining things, hope that helps!

fumei
04-24-2008, 11:48 AM
Greg, try his code if you want, but as you state you are green at this....please read his warnings!

Note3: After a little more testing, this code seems to work, but I would break test it hard. I still don't have full confidence in it. So use at your own risk.

Also note, like a good programmer should, he restores things to their original state. Unless you want to permanently silent things, you could pass a string to the Sub for any message you want silent.

Public Sub TestSilentBox(msg As String)
.....
.. do the stuff then display message
MsgBox msg

... and restore

End Sub

Sub NoDing()
Call TestSilentBox ("whatever")

End SubThat way, you can use it silent when you want, and only when you want.

GregE
04-24-2008, 11:52 AM
Aaron:

The comments were great! Thanks again for the help. Much appreciated!

Greg

Oorang
04-24-2008, 12:17 PM
Again I agree with Fumei, this is pretty much a hack. I did it for fun. But for any serious application it's much better to just build your own Message Box form.

~Just because you can, doesn't mean you should:)

GregE
04-24-2008, 12:41 PM
I'll look into building my own Message Box form.

It's all good when you're learning something new.

Thanks... Greg

Oorang
04-24-2008, 08:50 PM
lol Yah, I thought of another drawback to this approach. The sound remains disabled for all apps until the user responds to the message box.

fumei
04-25-2008, 11:37 AM
"~Just because you can, doesn't mean you should"


AMEN to that!!

"The sound remains disabled for all apps until the user responds to the message box."

Which may (or may not) actually be a good thing. They should be responding to the message as the priority, heck it has focus anyway. In any case, you could still use it to pass selected messages by having it require a string as a parameter.