关闭userform时终止sndPlaySound32

时间:2018-02-23 12:37:50

标签: excel-vba vba excel

我有一个主用户形式的项目。某些子用户窗体在打开时会播放一个很长的wave文件。但是,如果用户关闭子表单,则声音会继续播放。我找不到终止声音事件的方法而不用它杀死所有项目用户表单。我想关闭触发声音的子表单,听不到声音,但继续处理其他表单。怎么做到这一点?以下是我目前的代码。

Option Explicit
Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Sub UserForm_Activate()
    Application.ScreenUpdating = False
    sndPlaySound32 "F:\Program\Music\rainbow.wav", 1
End sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)    
    Application.ScreenUpdating = False

    Dim FCR As Object
    Set FCR = FrmCredentials

    Unload FCR
    Set FCR = Nothing

    Load FrmIndex
    FrmIndex.Show
End Sub

1 个答案:

答案 0 :(得分:2)

你可以停止玩

sndPlaySound32 vbNullString, &H1

sndPlaySound32 "NULL", &H1

以下是一个完整的例子:

Option Explicit

#if VBA7 then 'make it 64bit office compatible
    Public Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
#else
    Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
#end if

'  flag values for uFlags parameter
Const SND_SYNC = &H0            '  play synchronously (default)
Const SND_ASYNC = &H1           '  play asynchronously

Const SND_NODEFAULT = &H2       '  silence not default, if sound not found

Const SND_MEMORY = &H4          '  lpszSoundName points to a memory file
Const SND_ALIAS = &H10000       '  name is a WIN.INI [sounds] entry
Const SND_FILENAME = &H20000    '  name is a file name
Const SND_RESOURCE = &H40004    '  name is a resource name or atom
Const SND_ALIAS_ID = &H110000   '  name is a WIN.INI [sounds] entry identifier

Const SND_ALIAS_START = 0       '  must be > 4096 to keep strings in same section of resource file

Const SND_LOOP = &H8            '  loop the sound until next sndPlaySound
Const SND_NOSTOP = &H10         '  don't stop any currently playing sound
Const SND_VALID = &H1F          '  valid flags          / ;Internal /

Const SND_NOWAIT = &H2000       '  don't wait if the driver is busy

Const SND_VALIDFLAGS = &H17201F '  Set of valid flag bits.  Anything outside
                                '  this range will raise an error
Const SND_RESERVED = &HFF000000 '  In particular these flags are reserved

Const SND_TYPE_MASK = &H170007


Private Sub play()
    sndPlaySound32 "C:\Temp\spring.wav", SND_ASYNC
End Sub

Private Sub halt()
    sndPlaySound32 vbNullString, SND_ASYNC
End Sub