Silent Msgbox?

J

JoeU2004

Any way to do Msgbox without any sound?

I usually have most sounds disabled. So I was not aware that the following
statement causes a Default Beep sound to occur:

Msgbox "my information"

I experimented with other "button" constants -- vbCritical, vbExclamation,
vbInformation and vbQuestion. The Msgbox is silent for me; but I suspect
that is simply because I have the associated sound disabled. Moreover,
those "button" constants change the appearance of the msgbox. I don't want
that.

I wish there were a vbSilent, or some trick that had the same effect.

FYI, I am using VB 6.3 with Excel 2003 on Windows XP SP3.
 
B

Barb Reinhardt

I've not ever done this, but you may want to try

Application.EnableSound = False 'To Turn if off
Application.EnableSound = True 'To turn back on.

HTH,
Barb Reinhardt
 
J

JoeU2004

Barb Reinhardt said:
I've not ever done this, but you may want to try
Application.EnableSound = False 'To Turn if off
Application.EnableSound = True 'To turn back on.

Gee, I thought that would work, too. But surprisingly, it does not. By
"not work", I mean: I hear the sound.

Even if it had worked, it would raise the question of whether the async
sound (e.g., for Msgbox) might still be playing when EnableSound=True is
executed, and I would hear the tail end anyway. I added a 5-sec delay
before setting EnableSound=True, just to be sure that is not a factor.

(I test by setting Default Beep to Windows XP Startup.wav. That plays for
about 4.85 sec on my computer.)

See my test procedure below. Initially, I tested it as a Sub. Later, I
tested it as a Function, which is invoked from an Excel cell. Neither
worked.

PS: Of course, I can just disable the speaker from my laptop keyboard. I
am just playing with sounds to flush out potential issues related to another
person's postings here. It's the principle of the matter.


Public Declare Sub Sleep Lib "kernel32" (ByVal msec As Long)
Public Declare Function QueryPerformanceFrequency Lib "kernel32" (ByRef freq
As Currency) As Boolean
Public Declare Function QueryPerformanceCounter Lib "kernel32" (ByRef cnt As
Currency) As Boolean

Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

#Const testMsgbox = True

Function doit()
Dim freq As Currency, sc As Currency, ec As Currency
QueryPerformanceFrequency freq
Application.EnableSound = False
QueryPerformanceCounter sc
#If testMsgbox Then
' press Enter ASAP
MsgBox "press Enter"
#Else
sndPlaySound "Default Beep", 0
#End If
QueryPerformanceCounter ec
Debug.Print Format((ec - sc) / freq * 1000, "0.000000 msec")
Sleep 5000
Application.EnableSound = True
End Function


----- original message -----
 
J

John Keith

PS: Of course, I can just disable the speaker from my laptop keyboard. I
am just playing with sounds to flush out potential issues related to another
person's postings here. It's the principle of the matter.

Maybe the thread where I jumped in, "Beep not working".

Yeah, it's the principle of the thing for me too. I know it should
work, but for months (off and on when I see a hint or tip, like this
thread) I've never been able to find anything to get it to work!!!

I have one macro that a co-worker runs once a week and it takes 10-12
minutes to complete and I'd like to use a beep to signal successful
completion but I guess the message box will have to do until the
mystery is solved.

PS - I think I've tried the Application.EnableSound = False/True with
no success, I'd have to look at my notes I'm keeping on the issue.




John Keith
(e-mail address removed)
 
J

JoeU2004

John Keith said:
Yeah, it's the principle of the thing for me too. I know it should
work, but for months (off and on when I see a hint or tip, like this
thread) I've never been able to find anything to get it to work!!!

No, your problem is completely different. And there is no principle
involved.

You seem to be unable to find the "Default Beep" in the Sound control panel.
Obviously, if you cannot do that, and if Default Beep is configured to be
None, there is nothing you can until you learn how to assign a sound to
"Default Beep".

I have no problem finding "Default Beep" in the Sound control panel.

So I would appreciate it -- it just good netiquette -- if you would limit
your discussion of that problem to the thread where you started it, rather
than derail other threads with your unrelated problem.

For anyone who would like to respond to John, see the thread "Beep not
working".


----- original message -----
 
H

Héctor Miguel

hi, Joe !

if you can live with a (little) "delay" pre&post to your msgbox show-up...

Private Declare Sub Mute Lib "user32" Alias "keybd_event" ( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)

Sub Silent_Message()
Mute &HAD, 0, 1, 0
MsgBox "Did you hear something ???"
Mute &HAD, 0, 1, 0
End Sub

hth,
hector.

__ OP __
 
J

JoeU2004

Héctor Miguel said:
if you can live with a (little) "delay" pre&post to
your msgbox show-up

Not quite sure what delay you are referring to. If you are referring to the
overhead for the Mute calls, well, that's almost nothing -- less than 50
microsec each on my computer, compared to 100+ millisec for the Msgbox,
depending on human response time to press OK.

However....

Mute &HAD, 0, 1, 0
MsgBox "Did you hear something ???"
Mute &HAD, 0, 1, 0

The answer is: "maybe".

It depends on the length of the (asynchronous) sound associated with
"Default Beep". As I have said elsewhere in this thread, for testing
purposes, I associate the Windows XP Startup.wav sound, which goes on for
about 4.85 sec.

So your suggestion works "reliably" only if there is a Sleep call between
the Msgbox and second Mute calls; and then, only if the Sleep call is "long
enough".

Therein lies the problem with approaches like this: "long enough" depends
on the sound being played. It must be about 5000 millisec (give or take a
few) for the Startup.wav sound.


By the way, the code fragment above visibly toggles the speaker mute state,
as expected.

(Aside.... The code fragment works as written only if the speaker is not
muted in the first place. Is there a way to ascertain the mute state
beforehand?)

But the following does not seem to:

Mute &HAD, 0, 1, 0
sndPlaySound "Default Beep", 0
Mute &HAD, 0, 1, 0

Can anyone explain that to me? Just curious.

One theory: sndPlaySound turns off or toggles mute itself. I disproved
that theory by removing the Mute calls, and starting out with the speaker
muted. The sndPlaySound call was silent.


----- original message -----
 
H

Héctor Miguel

hi, Joe !

_____
However....
The answer is: "maybe".

It depends on the length of the (asynchronous) sound associated with "Default Beep".
As I have said elsewhere in this thread, for testing purposes
I associate the Windows XP Startup.wav sound which goes on for about 4.85 sec.

I found your previous post, where you made a mention regarding "startup.wav sound"
(how to tell when beep sound is done?)

_____
By the way, the code fragment above visibly toggles the speaker mute state, as expected.

yes indeed, I forgot to mention that (I apologizes) :D

_____
(Aside... The code fragment works as written only if the speaker is not muted in the first place.
Is there a way to ascertain the mute state beforehand?)

according to this MS-KB article:
SAMPLE: Volume.exe: Set Volume Control Levels Using Visual Basic
http://support.microsoft.com/default.aspx?scid=kb;en-us;178456

take a look on this thread: http://tinyurl.com/acflt
(pay special attention to replies 7 & 11)

_____
But the following does not seem to:

Mute &HAD, 0, 1, 0
sndPlaySound "Default Beep", 0
Mute &HAD, 0, 1, 0

Can anyone explain that to me? Just curious.

One theory: sndPlaySound turns off or toggles mute itself.
I disproved that theory by removing the Mute calls, and starting out with the speaker muted.
The sndPlaySound call was silent.

as Chip told you in your previous post, "sndPlaySound ..., 0" (or 1) is for sync/async play mode:
sync: code execution waits (all the time) untill sound finishes to go on
async: code execution continues, disregarding the time-to-go (untill the sound termination)

hth,
hector.
 
J

JoeU2004

Héctor Miguel said:
hi, Joe !

First, thanks, Hector, for your comments.

But the following does not seem to:

Mute &HAD, 0, 1, 0
sndPlaySound "Default Beep", 0
Mute &HAD, 0, 1, 0
[....]
as Chip told you in your previous post, "sndPlaySound ..., 0"
(or 1) is for sync/async play mode:

I don't see how that relates to my question.

Yes, I am playing the sound synchronously. That is my intent. Since I
associate Windows XP Startup.wav with the "Default Beep" sound, the
sndPlaySound call should take about 4.85 sec. (And it does, as demonstrated
by the macro doit3() below.)

My question is: why don't I see and hear the volume muted for the duration
of the sndPlaySound call, nearly 5 sec?

Let me be clear....

Try each macro below. Each macro gives you the opportunity to mute or
unmute the speaker initially. Normally, I unmute it initially. And
remember to associate Windows XP Startup.wav or the equivalent on your
computer with the "Default Beep" sound.

Why the difference in behavior between doit1() and doit2()?

That is, why does "Mute VK_VOLUME_MUTE,0,1,0" seem to fail to toggle the
volume off, then on around the sndPlaySound call, whereas it does just that
around the Msgbox call?

Moreover, in doit3(), why does the second pair of Mute calls, around the
last Msgbox call, leave the mute state set to the opposite of the initial
mute state?


Caveat: I have executed doit3() dozens of times, and it had consistently
misbehaved per my last question. But in the last 10 executions just now,
doit3() behaved "as expected" 2 or 3 times. That is, it seemingly failed to
toggle mute around the sndPlaySound, a misbehavior that we expect based on
doit2(); but it did correctly toggle mute around the second Msgbox call,
which is only a surprise because of the misbehavior that I have observed
dozens of times.

So the last problem exhibited by doit3() might not be as consistent I first
thought. If you cannot duplicate the misbehaviors, I suggest that you try 2
or 3 more times.


Design notes:

1. Yes, the second pair of Mute calls in doit3() are superfluous, since I
use Msgbox vbInformation, which does not play "Default Beep" sound. I left
them there only because they demonstrate an anomalous behavior, namely my
last question above.

2. I put some delay (Sleep 1000) after each Mute call for two reasons.
First, just in case the Mute operation is asynchronous. (I don't think it
is.) Second, to permit us to see a change of state reflected in the speaker
icon on the desktop toolbar, if you display it. A 1-sec delay is enough
time to accomplish both goals, especially the second one.


Public Declare Sub Sleep Lib "kernel32" (ByVal msec As Long)
Public Declare Function QueryPerformanceFrequency Lib _
"kernel32" (ByRef freq As Currency) As Boolean
Public Declare Function QueryPerformanceCounter Lib _
"kernel32" (ByRef cnt As Currency) As Boolean

Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Public Const VK_VOLUME_MUTE = &HAD

Public Declare Sub Mute Lib "user32" Alias "keybd_event" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)


Sub doit1()
' toggles mute off, then on as expected.
' no sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
MsgBox "press Enter"
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub


Sub doit2()
' seems to fail to toggle mute off, then on.
' produces sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
sndPlaySound "Default Beep", 0
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub


Sub doit3()
Dim sc As Currency, ec As Currency, freq As Currency

' seems to fail to toggle volume off, then on.
' produces sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
QueryPerformanceCounter sc
sndPlaySound "Default Beep", 0
QueryPerformanceCounter ec
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000

' suprise!
' leaves speaker muted if unmuted initially,
' or unmuted if muted initially.
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
QueryPerformanceFrequency freq
MsgBox "sndPlaySound duration: " & _
Format((ec - sc) / freq * 1000, "0 msec"), _
vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub


----- original message -----
 
H

Héctor Miguel

hi, Joe !
Yes, I am playing the sound synchronously. That is my intent.
Since Iassociate Windows XP Startup.wav with the "Default Beep" sound
the sndPlaySound call should take about 4.85 sec.
(And it does, as demonstrated by the macro doit3() below.)

My question is: why don't I see and hear the volume muted for the duration of the sndPlaySound call, nearly 5 sec?

1) I'm affraid not quite following your development considering that the goal is to get a "silent MsgBox"
if this is correct, calling the "keybd_event" api (pre & post a MsgBox) seems is doing the thrick (?)
(disregarding if msgbox is launched "alone" or vbInformation(ed) or with some other icon)

in this case, I guess that (possible) questions are:

a) how to be sure that volume is (un)muted prior to call the api ?
b) how to be sure that "default beep" is (not) assigned to a "not so short" sound file ?

so (I guess), we have to use something like Ivan' approach mentioned in the previous thread link (?)

2) I'm sure that doing a deeper R&D we can find the answers to your questions (and maybe others "in the way")
however, using recursive calls to sleep makes mi think that you expect a bahaviour like sendkeys method
while a vb/a code comunicates with other devices (?), so...

adding some MsgBox(es) after each sleep call let's you see (in taskbar) the mute icon toggling
(un)comment these new lines to compare the differences, and my tests over doit3() are as follows:

Sub doit3()

Dim sc As Currency, ec As Currency, freq As Currency

' seems to fail to toggle volume off, then on.
' produces sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
'MsgBox "was the volume mute toggled ?"

QueryPerformanceCounter sc
sndPlaySound "Default Beep", 0
QueryPerformanceCounter ec
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
'MsgBox "was the volume mute toggled ?"

' suprise!
' leaves speaker muted if unmuted initially,
' or unmuted if muted initially.
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
'MsgBox "was the volume mute toggled ?"

QueryPerformanceFrequency freq
MsgBox "sndPlaySound duration: " & _
Format((ec - sc) / freq * 1000, "0 ""msec"""), _
vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
'MsgBox "was the volume mute toggled ?"

End Sub

hth,
hector.

__ snip(ped) OP __
 

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