Can excel import file properties from an audio file (not tag data)?

Event2020

Board Regular
Joined
Jan 6, 2011
Messages
118
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone

If you right click on an audio file wav (it has to be a wav file) file, go to properties, then Audio Properties, can excel read and import the data to specific colums in a spread sheet?

This is different to tag data which relies on someone inputting the correct information in a tag editor.

The important fields in the Audio properties for me are: Length (minutes & seconds, Channels (mono or stereo), Sample Rate, Sample Size, Bit Rate and File.

This is way over my head.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Standard file I/O should do in order to retrieve the wav info from the header Bytes .


Canonical WAVE file format.jpg-1230x0.jpg



This is an adaptation from the code found here :

VBA Code:
Sub PrintWaveFileFormat(ByVal sWavFile As String)

    Dim MyInt As Integer
    Dim MyByte As Byte
    Dim MyStr As String * 4
    Dim MyLong As Long
    Dim SampleRate, BytesPerSample, FileSize As Long, Wavlength As Long

    If Len(Dir(sWavFile)) Then
        Open sWavFile For Binary Access Read As #1
            Get #1, , MyStr:    Debug.Print "Riff = "; MyStr
            Get #1, , MyLong:   Debug.Print "File size = "; MyLong
            FileSize = MyLong
            Get #1, , MyStr:    Debug.Print "Wave = "; MyStr
            Get #1, , MyStr:    Debug.Print "Format = "; MyStr
            Get #1, , MyLong:   Debug.Print "Any = "; MyLong
            Get #1, , MyInt:    Debug.Print "formatTag = "; MyInt
            Get #1, , MyInt:    Debug.Print "Channels = "; MyInt
            Get #1, , MyLong:   Debug.Print "Samples per Sec = "; MyLong
            SampleRate = MyLong
            Get #1, , MyInt:    Debug.Print "Bytes per Sec = "; MyInt
            Get #1, , MyInt:    Debug.Print "BlockAlign = "; MyInt
            Get #1, , MyInt:    Debug.Print "Bytes per Sample = "; MyInt
            BytesPerSample = MyInt
        Close #1
        Wavlength = FileSize \ (SampleRate * BytesPerSample)
        Debug.Print "Wavlength"; Wavlength
    Else
        MsgBox "File not found!"
    End If

End Sub

Usage example:
VBA Code:
Sub Test()
    Call PrintWaveFileFormat("C:\Windows\Media\chimes.wav")
End Sub
 
Upvote 0
Hi jaafar

Thank you for kindly taking the time to provide such an informative reply which I appreciate.

I am sorry to report that it does not seem to work, allow me to explain.
When I run the sub, it appears to run but it does not place any of the information it reads into the worksheet.
To test that the sub is able to find the wav file I renamed it to Test1.wav and this produced an error message
box "File Not Found" so it appears it can find the file but it is just not writing the required data to the worksheet.

I placed your sub on a worksheet named Sheet1.
I created a test wave file named test.wav in a folder on my C: Drive named temp.
I altered your Test call sub to reflect the path and file name of my test wav file.

I selected cell A1 in the above worksheet and then called your sub using a macro

VBA Code:
Sub Test()
    Call PrintWaveFileFormat("C:\Temp\Test.wav")
End Sub

I then tried

VBA Code:
Sub Test()
Call Worksheets("Sheet1").Call PrintWaveFileFormat("C:\Temp\Test.wav")
End Sub

I am afraid that both times the sub appeared to run but without writing any data.
 
Upvote 0
The PrintWaveFileFormat routine is actually sending the output to the immediate window in the VBE (not to the sheet)

Here is the same routine which is now modified to send the info to sheet1 in columns (A:B)

VBA Code:
Option Explicit

Sub PrintWaveFileFormat(ByVal sWavFile As String)

    Dim MyInt As Integer
    Dim MyByte As Byte
    Dim MyStr As String * 4
    Dim MyLong As Long
    Dim SampleRate, BytesPerSample, FileSize As Long, Wavlength As Long

    If Len(Dir(sWavFile)) Then
        With Sheet2
            Open sWavFile For Binary Access Read As #1
                Get #1, , MyStr:  .Cells(1, 1) = "Riff:": .Cells(1, 2) = MyStr
                Get #1, , MyLong:   .Cells(2, 1) = "File size :": .Cells(2, 2) = MyLong
                FileSize = MyLong
                Get #1, , MyStr:  .Cells(3, 1) = "Wave:": .Cells(3, 2) = MyStr
                Get #1, , MyStr:  .Cells(4, 1) = "Format:": .Cells(4, 2) = MyStr
                Get #1, , MyLong:   .Cells(5, 1) = "Any :": .Cells(5, 2) = MyLong
                Get #1, , MyInt:   .Cells(6, 1) = "formatTag :": .Cells(6, 2) = MyInt
                Get #1, , MyInt:   .Cells(7, 1) = "Channels :": .Cells(7, 2) = MyInt
                Get #1, , MyLong:   .Cells(8, 1) = "Samples per Sec :": .Cells(8, 2) = MyLong
                SampleRate = MyLong
                Get #1, , MyInt:   .Cells(9, 1) = "Bytes per Sec :": .Cells(9, 2) = MyInt
                Get #1, , MyInt:   .Cells(10, 1) = "BlockAlign :": .Cells(10, 2) = MyInt
                Get #1, , MyInt:   .Cells(11, 1) = "Bytes per Sample :": .Cells(11, 2) = MyInt
                BytesPerSample = MyInt
                Close #1
                Wavlength = FileSize \ (SampleRate * BytesPerSample)
                .Cells(12, 1) = "Wavlength:": Cells(12, 2) = Wavlength
        End With
    Else
        MsgBox "File not found!"
    End If

End Sub

Then like before :
VBA Code:
Sub Test()
    Call PrintWaveFileFormat("C:\Temp\Test.wav")
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,665
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top