Displaying a video in Excel

TotalBeginner201

New Member
Joined
Oct 24, 2021
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

Here is my problem. I want to display a video file with *.avi or *.mp4 extension using mciSendString.The file is located on my hard drive. I've already found two methods posted on mrexcel:
I used both of them, changing the api declarations by adding ptrsafe. I also put my file's full path.

First approach modificated with ptrsafe in code module:

VBA Code:
Public Declare PtrSafe Function sndPlaySound32 _
    Lib "winmm.dll" _
    Alias "sndPlaySoundA" ( _
        ByVal lpszSoundName As String, _
        ByVal uFlags As Long) As Long

Public Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String _
, ByVal lpstrReturnString As String, ByVal uReturnLength As Long _
, ByVal hwndCallback As Long) As Long


Declare PtrSafe Function GetActiveWindow Lib "USER32" () As Integer

Const WS_CHILD = &H40000000

Sub PlayAVIFile()

'Dimension variables.
Dim CmdStr As String, FileSpec As String
Dim Ret As Integer, XLSHwnd As Integer

'The name and location of the AVI file to play.
FileSpec = "C:AVIFilesE-MailScanemail.avi" 'C:AVIFilesCommconnectmodemld.avi"

'Get the active sheet's window handle.
XLSHwnd = GetActiveWindow()

'Opens the AVIVideo and creates a child window on the sheet
'where the video will display. "Animation" is the device_id.
CmdStr = ("open " & FileSpec & _
" type AVIVideo alias animation parent " & _
LTrim$(Str$(XLSHwnd)) & " style " & LTrim$(Str$(WS_CHILD)))

Ret = mciSendString(CmdStr, 0&, 0, 0)

'Put the AVI window at location 25, 120 relative to the
'parent window (Microsoft Excel) with a size of 160 x 160.
Ret = mciSendString("put animation window at 50 240 160 160", _
0&, 0, 0)

'The wait tells the MCI command to complete before returning
'control to the application.
Ret = mciSendString("play animation wait", 0&, 0, 0)

'Close windows so they don't crash when you exit the application.
Ret = mciSendString("close animation", 0&, 0, 0)


End Sub

The code executes. I am not getting any errors. However nothing happens.
Everything runs fine when I try to play an mp3. I can hear the mp3 file being played:

VBA Code:
Sub PlaympFile()


Dim CmdStr As String, FileSpec As String
Dim Ret As Integer, XLSHwnd As Integer

FileSpec = "C:AVIFilesE-MailScanemail.avi"


XLSHwnd = GetActiveWindow()

CmdStr = "open " & FileSpec & " alias animation"

Ret = mciSendString(CmdStr, 0&, 0, 0)


Ret = mciSendString("play animation wait", 0&, 0, 0)

Ret = mciSendString("close animation", 0&, 0, 0)


End Sub

The second approach involves using an userform. I tried it with userform_click event. In userform module i put the following:

VBA Code:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

Private Sub UserForm_Click()
Dim FrmHwnd As String
Dim Ret, L, T, W, H  As Long

With Me.Image1
    L = .Left
    T = .Top
    W = .Width
    H = .Height
End With

FrmHwnd = FindWindow(vbNullString, Me.Caption) & " Style " & &H40000000
strCommand = "open C:\MyAVI.avi Type avivideo Alias video1 parent " & FrmHwnd
    
    Ret = mciSendString(strCommand, "", 0, 0)
    Ret = mciSendString("put video1 window at", L & T & W & H, 0, 0)
    Ret = mciSendString("play video1 wait", "", 0, 0)
    Ret = mciSendString("close video1", "", 0, 0)
End Sub

Still nothing happens .

In Multimedia:Playing a Device there is info that:
"
Your application can specify the following options to define the playback window for playing an AVI file:


  • Use the MCIAVI driver's default pop-up window.
  • Specify a parent window and window style that the MCIAVI driver can use to create the playback window.
  • Specify a playback window for the MCIAVI driver to use for playback.
  • Play the AVI file on a full-screen display.
"
and

"When the MCIAVI driver creates the playback window or obtains a window handle from your application, it does not display the window until your application either plays the sequence or sends a command to display the window. Your application can use the window command to display the window without playing the sequence. For example, the following command displays the window using mciSendString"

I know that there is a windows media player control. I used it and it works ok. I prefer something like mci because it doesn't require the user of workbook to add reference.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This plays an MP4 here :

VBA Code:
Option Explicit

Sub playMP4()

Dim WMP As Object
    Set WMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")
    WMP.OpenPlayer "C:\Users\jimga\Desktop\Movies\12-Mighty-Orphans-2021-STAGATV-COM.mp4"
End Sub
 
Upvote 0
Thank you Logit. This solution works and after running this macro a windows media player shows up and the video is being displayed. I'm trying to do something like a splash screen/intro that runs on opening of a workbook or use an userform as a parent for that video. Would it be possible to play a video using Your method with UI disabled or in some kind of windowless mode? In object viewer I can see that WindowsMediaPlayer class has properties like uiMode or windowlessVideo. Seems like i can only read values of those properties. When I use something like:
VBA Code:
Sub playMP4()

Dim WMP As Object
    Set WMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")
    WMP.windowlessVideo = True
    'Debug.Print WMP.windowlessVideo
    WMP.OpenPlayer "path to file goes here"
    
End Sub
or
VBA Code:
Sub playMP4()

Dim WMP As Object
    Set WMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")
    WMP.uiMode = "none"
    WMP.OpenPlayer "path to file goes here"
    
End Sub

it still displays the media player with all the controls. Nothing changes.
wmpquestion.png
 
Upvote 0
Splash screen with video?

Post #2 in the above URL link contains everything you should need to run a video in a splash screen.
I did not post any code here as it is quite extensive.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top