Open Ole Object with VBA in front of the Excel Window

I have an excel spreadsheet with a video that is embeded as an OleObject.  I have code set on a button to open/activate the video. The code works fine except that sometimes the video opens in front of the excel window (as I want it to)  and sometimes it opens behind the window.  Is there code I can use to specify that it should open in front?

The code I have now is just:

Private Sub CommandButton2_Click()


End Sub
Who is Participating?

pls try change line 5 for the name of the window

Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub WindowToFront()
    hWnd = FindWindow(vbNullString, "Video")
    If hWnd > 0 Then
        SetForegroundWindow (hWnd)
    End If
End Sub

Open in new window

amy_goeAuthor Commented:
That's seems to have done it.  Thank  you!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.