Windows Media Player reference and VBA

Related to my genealogy and photography hobby, I write software to document media including jpeg files (see ‘DocuPhoto’) and now WMA and other audio files.    Both took a lot of research and documenting WMA (digital audio files) was especially difficult.     Here is a method that allows a developer to programmatically populate the extended properties of an audio file.  These are the fields you see when you use Windows Explorer, right-click on a filename, select ‘Properties’ or ‘Details’ and you see the fields “Title”, “Subtitle” (or “Subject”), “Author”, “Comments”, etc…   These fields are the only way to “write on the back of the photo” or to “label” your media so I think it is extremely important for archivists and genealogists to be able to manipulate these fields.   This VBA code works for WMA files and it may work for other audio files also.  It was written with digital audio files from interviews and speeches (not music audio files).

These instructions are specifically for Windows 7 operating system for VBA with Excel 2010 but much of it will be applicable to earlier versions of Excel and Visual Basic 6.0 and Visual Basic .NET:

1. In the Developer ribbon, open up the Visual Basic editor.

2. Go to ’Tools’ and ‘References’ and browse to “c:\Windows\System32\wmp.dll”.   Check it to add it to your references.  

3.  If you do NOT find this dll, you need to add the latest Windows Media Player application to your computer.

4. Here’s the weirdness: you must add the wmp object to a UserForm – you apparently can not access the control directly.    Create your UserForm with a button to start your application.   Select the toolbox icon (the crossed hammer and wrench) to open up the standard toolbox box.

5.  Right click in the gray area of the toolbox.  You will see the option “Additional controls…”

6.  Select the “WindowsMediaPlayer” icon in the toolbox.   Now drag it open to your UserForm.  Make it as small as possible and you can make its property “invisible”.

7.  Now you can access the WMP ActiveX functions in your code.   Create or go to a module to write your code.  My example code is below.

8.  Microsoft operating system weirdness:  the Windows properties “Comments” is modified by the software using the wmp attribute “Description”.   However all of the other parameters may not change even though no error is thrown by the program.  This can be caused by the folder security.   Be sure “Users” are allowed full rights to the folder that the WMA file is residing.  I’m still investigating this issue but apparently, even on my own laptop, with folders that I created manually, I do not always have full rights to modify the WMA files using the wmp.dll object programmatically.

===============  VBA CODE ==================

Public Function SetWMAFileProperties(sFile As String, sTitle As String, _
  sAuthor As String, sCopyright As String, sDescription As String, _
  sSubTitle As String, sAlbumArtist As String, sAlbumTitle As String, _
  sYear As String, sGenre As String, sContentGroupDescription As String, _
  sProducer As String, sPublisher As String) As Boolean

‘ WMA files are handled totally differently from JPEG.
‘ PROGRAMMER: You must add a reference to “wmp.dll” and then put the object on your form.

‘ Here are a list of attributes:
‘  
http://msdn.microsoft.com/en-us/library/dd743066(v=vs.85).aspx
‘ Here is MSDN’s WMA SDK.
‘  
http://msdn2.microsoft.com/en-us/library/aa390668.aspx
‘——————————————————————————
 Const sProcName As String = “SetWMAFileProperties()”‘
 Dim wmp As WindowsMediaPlayer
 Dim sAttribute As String
 Dim i As Long
 Dim bFlag As Boolean
 ’
 Set wmp = fDocuPhoto.WindowsMediaPlayer1  ‘ note that you must reference your UserForm name here
 wmp.settings.autoStart = False     ‘ this will keep your audio file from playing when it is accessed by the code
 ’
 Dim oMedia As IWMPMedia
 Set oMedia = wmp.newMedia(sFile)
 ’
 ’ For programming development purposes. Generates the official list of attributes.
 For i = 0 To oMedia.attributeCount – 1
   sAttribute = Trim$(oMedia.getAttributeName(i))
   bFlag = oMedia.isReadOnlyItem(sAttribute)
   Debug.Print Format$(i, “000″) & ” <” & sAttribute & “> ” & bFlag & ” = ” & oMedia.getItemInfo(sAttribute)
 Next i
 ’
 On Error GoTo ErrorHandler
 ’
 Call oMedia.setItemInfo(“Title”, sTitle)   ‘ Windows ‘Title’
 Call oMedia.setItemInfo(“Author”, sAuthor)              ‘ Windows ‘Contributing Artists’
 Call oMedia.setItemInfo(“Copyright”, “created: ” & Format$(Now, “yyyy-mmm-dd hh:nn”)) ’
 Call oMedia.setItemInfo(“Description”, sDescription)    ‘ Windows ‘Comments’
 Call oMedia.setItemInfo(“Subtitle”, sSubTitle)          ‘ Windows ‘Subtitle’
 Call oMedia.setItemInfo(“WM/AlbumArtist”, sAlbumArtist) ‘ Windows ‘Album Artist’
 Call oMedia.setItemInfo(“WM/AlbumTitle”, sAlbumTitle)   ‘ Windows ‘Album Title’
 Call oMedia.setItemInfo(“WM/Year”, sYear)               ‘ Windows ‘Year’
 Call oMedia.setItemInfo(“WM/Genre”, sGenre)             ‘ Windows ‘Genre’
 Call oMedia.setItemInfo(“WM/ContentGroupDescription”, sContentGroupDescription)
 Call oMedia.setItemInfo(“WM/Producer”, sProducer)       ‘ Windows ‘Producer’
 Call oMedia.setItemInfo(“WM/Publisher”, sPublisher)     ‘ Windows ‘Publisher’
 ’
 ’   I can’t get ‘Tags’ or ‘Ratings’ to work.  If anyone has a clue, let me know.’
 ’Call oMedia.setItemInfo(“Tags”, “new tags at ” & Format$(Now, “hh:nn:ss”))
 ’Call oMedia.setItemInfo(“Rating”, 3)
  ’
 SetWMAFileProperties = True
 ’
 ’ For programming development purposes. Generates the official list of attributes.
 Debug.Print ” ”
 Debug.Print ” ***** AFTER ******”
 For i = 0 To oMedia.attributeCount – 1
   sAttribute = Trim$(oMedia.getAttributeName(i))
   bFlag = oMedia.isReadOnlyItem(sAttribute)
   Debug.Print Format$(i, “000″) & ” <” & sAttribute & “> ” & bFlag & ” = ” & oMedia.getItemInfo(sAttribute)
 Next i

 GoTo Drain
 ’
ErrorHandler:
 Select Case Err.Number
 Case 70   ‘ permission denied
   SetWMAFileProperties = False   ‘ this is probably not a WMA or other audio file
   GoTo Drain
 Case Else
   MsgBox “ERROR in ‘” & sProcName & “‘: ” & Err.Number & ” ” & Err.Description
 End Select
 ’
Drain:
 Set oMedia = Nothing
 Set wmp = Nothing
 End Function

2 Responses to Windows Media Player reference and VBA

  1. Maralynn says:

    It’s about time smenooe wrote about this.

  2. Rex Ryan says:

    My brother suggested I might like this blog. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>