Macro for playing an audio file only twice

C

camfont

Hello there,

I need to create a macro wherein an audio file is played when an icon is
clicked in a Word document. After the audio has played, the same icon can be
clicked to hear the audio again. However, after the second listen, the icon
should become disabled/the audio shouldn't be allowed to play again. The
reason for this is that it's for a quiz in which I only want students to hear
the audio a maximum of two times.

Many thanks.
 
G

Gordon Bentley-Mix

See http://gregmaxey.mvps.org/Sound_On_Document_Open.htm for info on the
macro for playing an audio file when a button is clicked. You'll need to
adapt it to your particular use, but it should get you started.

As for limiting it to two clicks, I suppose you could increment a counter
when the button is clicked and evaluate this counter to determine the proper
response. Exactly how you would do that would depend on several factors,
including just exactly what's being clicked (a toolbar button, a macrobutton,
etc.), if the restriction needs to be permanent or just for a particular
instance of the document being opened, what should happen on the third and
subsequent clicks, etc.

One possible approach, which sets the restriction to a particular instance
of the document being opened and displays a message box on the third and
subsequent attempts:

Option Explicit
Dim myCounter As Long

Sub ListenToMe()
myCounter = myCounter + 1
If myCounter < 3 Then
PlayAudioFile
Else
MsgBox "Sorry! You've already played the sound twice!", vbCritical
End If
End Sub

Private Sub PlayAudioFile()
' Replace the following with a modified version of Greg Maxey's code
MsgBox "TA-DA!!!"
End Sub

I launch this code from a button on a custom toolbar, but it could easily be
used with a macrobutton. I also display a message box, although I note that
your requirement is to disable the control; this is considerably more
involved - especially if you want to use a macrobutton.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
C

camfont

Hello Gordon,

Thank you for your response. I'm afraid I can't completely follow it,
however-- I'm pretty VBA illiterate. I put in a Macro button and added your
code in a probably haphazard way. Right now I have the following, but I get
an error (Compile error: only comments may appear after End Sub, End
Function, or End Property):

Private Sub CommandButton1_Click()

Sub PlaySound()

Option Explicit
Dim myCounter As Long

Sub ListenToMe()
myCounter = myCounter + 1
If myCounter < 3 Then
PlayAudioFile
Else
MsgBox "Sorry! You've already played the sound twice!", vbCritical
End If

Private Sub PlayAudioFile()

Option Explicit
Private Declare Sub PlaySound Lib "winmm.dll" _
(ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long)
Private Const Placement_Test_B_Listening_1 = &H20000

Sub AutoOpen()
Dim sndFileName As String
Dim i As Long
'Note: Change the next line to point to your sound file.
sndFileName = "Placement_Test_B_Listening_1.wav"
PlaySound sndFileName, 0&, Placement_Test_B_Listening_1

Private Sub Play_Click()

End Sub
 
G

Gordon Bentley-Mix

Probably easier if you contact me at the email address in my profile and I
send you an example document rather than trying to correct everything that's
wrong with your code. ;-D You might also want to send me your document so I
can make sure that what I'm mocking up fits with your desired outcome.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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