PDA

View Full Version : [SOLVED] Excel VBA with Windows Media Player: Runtime error while getting video timecode



Dicky
06-19-2017, 01:18 PM
I've added a Windows Media Player ActiveX control (WMPlayer2) and two command buttons, viz, cmd_PlayVideo and cmd_GetTimeCode on an Excel sheet. I would like to play a video in Windows Media Player and capture the timecode of the video as and when I click the cmd_GetTimeCode button while the video is being played.

Since the 'currentPositionTimecode' property is available in IWMPControls3 interface, I've cast the return value from WMPlayer2.controls to the IWMPControls3 interface. However, the line 'MsgBox controls.currentPositionTimecode' throws: run-time error 430: "Class does not support Automation or does not support expected interface." The other properties of the IWMPControls3 interface such as audioLanguageCount, currentAudioLanguage, currentItem, controls.getAudioLanguageID, getLanguageName, and getAudioLanguageDescription work fine.

How can I get the timecode of the video?
Here is my code in 2 buttons.
-------------------------------------------------------------------------


Private Sub cmd_PlayVideo_Click()

MsgBox Application.Sheets("Sheet1").Range("B2")
WMPlayer2.URL = Application.Sheets("Sheet1").Range("B2")

End Sub
-------------------------------------------------------------------------


Private Sub cmd_GetTimeCode_Click()

Dim controls As WMPLib.IWMPControls3
Dim Media As WMPLib.IWMPMedia
Set controls = WMPlayer2.controls

MsgBox controls.audioLanguageCount
MsgBox controls.currentAudioLanguage
If controls.isAvailable("currentItem") ThenSet Media = controls.currentItem
MsgBox Media.durationString
MsgBox Media.Name
MsgBox Media.SourceUrl
End If

MsgBox controls.getAudioLanguageID(1)
MsgBox controls.getLanguageName(1)
MsgBox controls.getAudioLanguageDescription(1)
MsgBox controls.currentPositionTimecode

End Sub

SamT
06-19-2017, 05:54 PM
First, I would never use "controls" as a variable name because it is a Key Word in VBA. I suspect that "Media" is also a Key Word.. I suggest "Ctrls" and "MdiaPlyr."

Try using currentPosition, a Double, or currentPositionString, a String, instead of currentPositionTimeCode, also a String. Just to see what happens.

THere is a slim chance that you must use IWMPControls or IWMPControls2.

Sometimes you have to cast many spells to see which one makes the pot boil. http://www.vbaexpress.com/forum/image.php?u=6494&dateline=1378078465 (http://www.vbaexpress.com/forum/member.php?6494-SamT)

Dicky
06-20-2017, 02:33 AM
Thanks SamT for your response. I tried both the 'currentPosition' and 'currentPositionString' properties and they seem to work fine.
I don't know how to cast the 'currentPositionTimeCode' into IWMPControls or IWMPControls2 interfaces. Also, the currentPositionTimeCode property is available only for IWMPControls3 interface, which is a member of WMPLib. The IWMPControls or IWMPControls2 interfaces do not have the currentPositionTimeCode property. Hence I didn't think of casting into IWMPControls or IWMPControls2. I would appreciate if you please show me how to cast into IWMPControls or IWMPControls2 interfaces in VBA.

SamT
06-20-2017, 06:07 AM
please show me how to cast into IWMPControls or IWMPControls2 interfaces in VBA.
They must be available in Tools >> References. then

Dim Ctrls As WMPLib.IWMPControls
and

Dim Ctrls As WMPLib.IWMPControls2

Dicky
06-20-2017, 08:32 PM
They must be available in Tools >> References. then

Dim Ctrls As WMPLib.IWMPControls
and

Dim Ctrls As WMPLib.IWMPControls2

I tried both IWMPControls and IWMPControls2 and I still get the same run-time error 430.
Any other suggestions are welcome.