开发者

play sound triggered by click on form control

I'm trying to trigger a sound by clicking on a form control in Excel 11. The handler for the click event t开发者_JS百科ries to play a sound using sndPlaySound32 if it finds certain text on the clipboard.

I've declared the function sndPlaySound32, and call it with (simplified)

whatSound$ = "C:\WINDOWS\Media\Office97\Drop.wav"  
sndPlaySound32 whatSound$, &H0

As long as Excel is the front app when I click the control then everything works fine and the sound plays (if the target text is found on the clipboard), but if I'm bringing the text in on the clipboard from another app like Notepad, where Excel is not the front app when the click occurs, then the first click on the control doesn't produce any sound. Subsequent clicks work normally. Other events on the form have no problem with this and continue to respond normally to the first click.

How can I make the sound play on the first click of the control when Excel is not the front app?

Edit 11/25/10: I can't make the board Comments work. I click Add Comment but my comments aren't shown, so this is a reply to Boost.

I think it's something deeper than just bringing Excel to the front with the first click, because there are several of other actions that are triggered successfully by this first click on the control, (while another app is in front), and they all work just fine. It's only the playSound that doesn't work until subsequent clicks.

I've tried moving the playSound command both to the front and to the end of the list of other actions (e.g. font changes, text resizing etc.) that are supposed to happen with each click, and also tried putting delays in various places, but no joy. If I step through the code, the playSound procedure gets called correctly on the first click but it doesn't produce a sound.


I don't think there's a fix here. An application will not accept mouse events until it is "in focus." If Notepad is in focus, you will transfer focus from it to Excel with the first click, then Excel's event handlers will come into play and respond to subsequent events.

You could (and I don't know if it's even possible) put a transparent windows over the whole screen and interpret mouse-clicks in that context, and pass them to the appropriate underlying windows. SMOP, I suppose.


I finally found a workaround that's simple, reliable, and ugly. Since it won't play the sound on the first click, I just give it another chance. So now, in place of the code in my original post, it looks like this:

whatSound$ = "C:\WINDOWS\Media\Office97\Drop.wav"  
sndPlaySound32 whatSound$, &H0
sndPlaySound32 whatSound$, &H0

It still ignores the first playSound command, but it plays the second one every time on the first click.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜