Open an AVI file from an Userform

watsonk0358

New Member
Joined
Apr 23, 2004
Messages
32
On a sheet in Excel I create an avi object, I can double click it to open up and play. I tried recording a macro, but when I put that macro in a userform it gives me an error. I have tried several searches and can't seem to find any topic for opening an AVI file. I want to use a command button on a userform that would start the avi file. Could someone help me with a macro that will start my avi file from a command button on a userform, my avi file is called "example". Does my avi file need to be on a sheet in Excel? Thanks you guys are the best.

Kim :oops:
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Don't know if this will help you or not and I'm sure there are other ways to do this but when I needed to play a video on opening a workbook I used the following.
I created an object, attached the video to it and then set is visible property to hidden, you can then unhide it and play it from a button click and also re-hide it.

Sheets("Sheet1").Shapes("Object 1").Visible = True
Sheets("Sheet1").Shapes("Object 1").Select
Selection.Verb Verb:=xlPrimary
Sheets("Sheet1").Shapes("Object 1").Visible = False
End Sub


Sorry if this is not what you wanted.

Dave.
 
Upvote 0
:oops: Dave, I opened Excel, went to Insert, Object, create from file and inserted my avi file. I then went to Tool, Option, View and hide all. To hide the object. When I keyed in the macro, and run it, I get an error, on the first line. Can you tell me if I am doing these steps right or is there another way to bring my avi files into Excel. Thanks for all of your help.

Kim
 
Upvote 0
Hi,

Place the code below in the Cick_Event of the Command1 control button on the UserForm Module.

The code assumes , you have an Image Control Image1 on which the AVI file is supposed to play.

Note: You will need to change the AVI file path (C:\MyAVI.avi) to suit your own.



Code:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> mciSendString <SPAN style="color:#00007F">Lib</SPAN> "winmm.dll" Alias _
    "mciSendStringA" (<SPAN style="color:#00007F">ByVal</SPAN> lpstrCommand <SPAN style="color:#00007F">As</SPAN> String, ByVal _
    lpstrReturn<SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> uReturnLength <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal _
    hwndCallback <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias _
"FindWindowA" (<SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> String, <SPAN style="color:#00007F">ByVal</SPAN> lpWindowName <SPAN style="color:#00007F">As</SPAN> String) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()

    <SPAN style="color:#00007F">Dim</SPAN> FrmHwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> Ret, L, T, W, H <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">With</SPAN> Me.Image1
        L = .Left
        T = .Top
        W = .Width
        H = .Height
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
    FrmHwnd = FindWindow(vbNullString, Me.Caption) & " Style " & &H40000000
    <SPAN style="color:#007F00">'</SPAN>
    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)

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>


Although the Code works, the AVI video doesn't locate itself on top of the Image1 control. Instead, it shows up at the Top Left corner of the UserForm !

Obviously I am doing something wrong . :unsure: I hope someone can figure out what.

Regards.
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,800
Members
449,189
Latest member
kristinh

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