簡體   English   中英

執行功能時停止VBA以阻止Excel工作表

[英]Stop VBA to block Excel sheet when executing a function

我有以下Excel方案:

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

但它們是可以互換的。

在工作簿路徑中,有兩個子文件夾,其中包含名為A.wav,B.wav和C.wav的wav文件。

底部的代碼使我可以單擊按鈕以觸發宏PlayIt()來播放wav文件。

我的問題是,當函數執行時,我無法在Excel中真正需要編輯單元格! 看起來像這樣

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

感謝您的任何幫助!

音頻播放代碼:

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

您有兩種錯誤的聲明方式: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM