I have a UserForm that contains a Windows Media Player object. When the UserForm is initialized it assigns the file URL to the media player object, sets its size and then plays the video. (I have disabled the autoplay feature within the {Custom} settings.) All of this code works correctly. The problem is when I attempt to close the UserForm I receive the following error: Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients.
To automatically close the UserForm I utilized the following code:
Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)
'MoviePlayer variable is set when UserForm initializes
Debug.Print MoviePlayer.Status
If MoviePlayer.Status = "Stopped" Then
Unload Me '<----Crash occurs following this action
End If
End Sub
Additional Relevant Code:
Private Sub UserForm_Initialize()
Set MoviePlayer = Me.WindowsMediaPlayer1
MoviePlayer.url = "My Video File"
MoviePlayer.uiMode = "None"
MoviePlayer.stretchToFit = True
MoviePlayer.Top = Me.Top + 1
MoviePlayer.Left = Me.Left + 1
MoviePlayer.Height = Me.InsideHeight - 2
MoviePlayer.Width = Me.InsideWidth - 2
MoviePlayer.Controls.Play
End Sub
UserForm is called from:
Sub TestMe()
MyUSerform.Show
End Sub
I have attempted to set the mediaplayer object to nothing, prior to Unload Me
, as well as clearing the associated url. However, this has not prevented the error from occurring. Additionally, I tried adding End
following the Unload Me
action which does eliminate the error but also crashes my Excel. On Error
has not helped either. The error does not occur when the UserForm is closed by the user.
I am using Excel 2013. As a side note, I used the above code on my personal Excel, version 2007, and I do not receive the automation error.
I would appreciate any assistance with code to get the userform to close without an error.