title: Play any audio file using VBA Excel
body: <p>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.</p>
  <p>so I have to revert to my other code which accepts it, but reads only wav files...</p>
  <p>this is the code for reading all type of audio:</p>
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  
  Private Declare PtrSafe Function mciSendString Lib &quot;winmm.dll&quot; Alias _
     &quot;mciSendStringA&quot; (ByVal lpstrCommand As String, ByVal _
     lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
     hwndCallback As Long) As Long
  
  Private sMusicFile As String
  Dim Play
  
  Public Sub Sound2(ByVal File$) 
  
  sMusicFile = File    'path has been included. Ex. &quot;C:\3rdMan.mp3
  
  Play = mciSendString(&quot;play &quot; &amp; sMusicFile, 0&amp;, 0, 0)
  If Play &lt;&gt; 0 Then 'this triggers if can't play the file
      'Play = mciSendString(&quot;'play &quot; &amp; sMusicFile &amp; &quot;'&quot;, 0&amp;, 0, 0) 'i tried this aproach, but doesn't seem to work
  End If
     
  End Sub
  
  
  Public Sub StopSound(Optional ByVal FullFile$)
  Play = mciSendString(&quot;close &quot; &amp; sMusicFile, 0&amp;, 0, 0)
  End Sub
  </code></pre>
  <p>Any help much appreciated, (I don't want workaround with external player popup, nor which I can't stop from playing with VBA)</p>