2017-07-13 64 views
0

我有以下Excel场景方框Excel表:停止VBA来执行时的功能

A1具有 “A” 的值,B2 = “B”,C3 = “C”

但它们是可互换的。

在工作簿路径有含名为A.wav,B.wav和C.wav

WAV文件两个子文件夹在底部的代码可以让我以回放按钮点击射击宏中的wav文件播放()。

我的问题是,虽然函数正在执行我无法编辑Excel中的单元格,我真的需要!它看起来有点像这样

https://gifyu.com/images/GIF8d5e1.gif

感谢您的帮助!

代码Audioplayback:

Option Explicit 

#If VBA7 Then 
Public Declare PtrSafe Function PlaySound Lib "winmm.dll" _ 
    Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long 
#Else 
Public Declare Function PlaySound Lib "winmm.dll" _ 
    Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long 
#End If 

Const SND_SYNC = &H0 
Const SND_ASYNC = &H1 
Const SND_FILENAME = &H20000 

Sub PlayTheSound(ByVal WhatSound As String) 
    If Dir(WhatSound, vbNormal) = "" Then 
     ' WhatSound is not a file. Get the file named by 
     ' WhatSound from the Windows\Media directory. 
     WhatSound = ThisWorkbook.Path & "\stimuli\" & "\1second\" & WhatSound 
     If InStr(1, WhatSound, ".") = 0 Then 
      ' if WhatSound does not have a .wav extension, 
      ' add one. 
      WhatSound = WhatSound & ".wav" 
     End If 
     If Dir(WhatSound, vbNormal) = vbNullString Then 
      Beep   ' Can't find the file. Do a simple Beep. 
      MsgBox "Could not find the file in the Path: " & WhatSound 
      Exit Sub 
     End If 
    Else 
     ' WhatSound is a file. Use it. 
    End If 

    PlaySound WhatSound, 0&, SND_ASYNC Or SND_FILENAME ' Finally, play the sound. 
End Sub 

Sub PlayIt() 
    PlayTheSound (Range("A1").Value) 
    PlayTheSound (Range("B1").Value) 
    PlayTheSound (Range("C1").Value & "e") 
End Sub 
+0

'BYVAL HMODULE作为LongPtr'。如果你的办公室是x64,可能是原因。 – GSerg

+0

@GSerg如果我删除'BYVAL HMODULE作为LongPtr'只有调用'PlaySound WhatSound,SND_ASYNC或者SND_FILENAME'我只听到最后的wav文件'PlayTheSound(范围( “C1”)。价值与 “E”)' – McTell

+0

你有'ByVal hModule As Long'。正确的事情是'ByVal hModule As LongPtr'。 – GSerg

回答

0

你有你的声明错误有两种方式:hModule应该LongPtr,虽然实际调用sndPlaySound您使用的PlaySound参数。

修复你的宣言:

#If VBA7 Then 
Public Declare PtrSafe Function PlaySound Lib "winmm.dll" _ 
    Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long 
#Else 
Public Declare Function PlaySound Lib "winmm.dll" _ 
    Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long 
#End If 
+0

这引发了另一个问题,现在只有PlayIt()的最后一行会播放PlayTheSound(Range(“C1”)。值&“e”)',第一个du行被“跳过”... – McTell

+0

对不起,但VBA noob问题; Alias实际上做了什么?我似乎可以安全地删除它,只有这样:'公共声明PtrSafe函数PlaySound库“winmm.dll”(012)提供现在更新。非常感谢! – McTell

+0

@McTell就像在文档中声明的那样,当你开始一个新的声音时,它会停止当前播放的声音,除非你也通过'SND_NOSTOP'(你不这样做),在这种情况下,如果另一个声音不会播放任何东西正在处理。当从库中导出的名称与代码中要用于该函数的名称不同时,将使用“别名”。实际的函数名称是'PlaySoundA',除非您愿意将其声明为'PlaySoundA',您必须提供别名。如果您提供与声明名称相匹配的别名,则IDE将删除它。 – GSerg