Play any audio file using VBA Excel

前端 未结 5 1594
独厮守ぢ
独厮守ぢ 2021-01-26 20:52

I have a piece of code which can read most audio files (including wav, mp3, midi...), but it won\'t work if there are spaces in the path or File name.

so I have to revert

5条回答
  •  滥情空心
    2021-01-26 21:47

    i found The work-around, that correct spaces in path name (and (edit) for file name (using copy of file with no spaces, ugly but works (name as would not be a good solution) :

    After the first attempt to play the sound, if fails i change the current directory to the sound directory (temporarely):

    If Play <> 0 Then 
    
        Dim path$, FileName0$
        path = CurDir
    
        If InStr(sMusicFile, ":") > 0 Then ChDrive (Left(sMusicFile, 1))
        If InStr(sMusicFile, "\") > 0 Then
            ChDir (Left(sMusicFile, InStrRev(sMusicFile, "\") - 1))
            FileName0 = Mid(sMusicFile, InStrRev(sMusicFile, "\") + 1)
            If InStr(FileName0, " ") > 0 Then
                FileCopy FileName0, Replace(FileName0, " ", "")
                sMusicFile = Left(sMusicFile, InStrRev(sMusicFile, "\")) & Replace(FileName0, " ", "")
                Play = mciSendString("play " & Replace(FileName0, " ", ""), 0&, 0, 0)
            Else
                Play = mciSendString("play " & FileName0, 0&, 0, 0) 
            End If
        Else
            FileName0 = Replace(sMusicFile, " ", "")
            If sMusicFile <> FileName0 Then
                FileCopy sMusicFile, FileName0
                sMusicFile = FileName0
            End If
            Play = mciSendString("play " & sMusicFile, 0&, 0, 0)
        End If
    
        ChDrive (Left(path, 1))
        ChDir (Left(path, InStrRev(path, "\") - 1))
    
    End If
    

    Note : for spaces in the name i got also a new method : Filecopy sMusicFile replace(sMusicFile," ","%") and then play this new file

提交回复
热议问题