1

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.

John W
  • 191
  • 2
  • 6
  • 14
  • 1
    Don't `Unload` a userform. `Me.Hide`. – BigBen Jan 29 '21 at 15:40
  • [In-depth discussion of why `Unload Me` is a bad idea](https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/). – BigBen Jan 29 '21 at 15:46
  • @bigben Thank you for the quick response. So me.hide kind of works but now my initialize code no longer runs when the userform is activated. Is there a way around this? Also, thank you for the link but the address is blocked by my company. – John W Jan 29 '21 at 15:52
  • @Bigben Added relevant code. So after some testing I think I got it to work. I left my `initialize.userform` code and added `activate.userform`. For that code I just included `movieplayer.controls.play` and it seems to work correctly now. Are there any ramifications with not unloading the userform? Will a different userform still show if the other never unloaded? – John W Jan 29 '21 at 16:24
  • Well what you really should do is create a new *instance* of the userform. The link I provided attempts to demonstrate that. Basically, you don't want `MyUSerform.Show`; instead, `With New MyUSerform`, `.Show`, `End With`. – BigBen Jan 29 '21 at 16:25

1 Answers1

2

Here is my solution based on the feedback from BigBen and the article by rubberduck:

In Module1:

 Sub NewTest()
    With New UserForm1
        Set MoviePlayer = .WindowsMediaPlayer1
        MoviePlayer.url = "My Video File"
        MoviePlayer.uiMode = "None"
        MoviePlayer.stretchToFit = True
        MoviePlayer.Top = .Top + 1
        MoviePlayer.Left = .Left + 1
        MoviePlayer.Height = .InsideHeight - 2
        MoviePlayer.Width = .InsideWidth - 2
        MoviePlayer.Controls.Play
        .Show
    End With
    End Sub

In UserForm1 I only have the following code:

Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)
    If WindowsMediaPlayer1.Status = "Stopped" Then
        Me.Hide
    End If
End Sub
John W
  • 191
  • 2
  • 6
  • 14