XL: Visual Basic Example to Play an AVI File

Last reviewed: September 2, 1997
Article ID: Q114039
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

SUMMARY

It is possible to use Visual Basic for Applications to play an audio video interleaved (AVI) file from Microsoft Excel. You can reference an AVI file directly without inserting it as an object.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Microsoft Excel versions 7.0 and 97

In Microsoft Excel versions 7.0 and 97, the following Visual Basic Code plays a Microsoft AVI file at a specified location and size on the active window. This example assumes that the file Blastoff.avi file is stored in the default folder (C:\Winvideo). This code does not require the full Microsoft Video package in order to function properly.

Sample Code:

'API and Constant Declarations
'The function declarations must each be typed on one line.

Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _
   lpstrCommand As String, ByVal lpstrReturnStr As Any, ByVal wReturnLen _
   As Long, ByVal hCallBack As Long) As Long

Declare 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:\WINVIDEO\BLASTOFF.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 25 120 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

Microsoft Excel versions 5.0 and 5.0c

In Microsoft Excel version 5.0, the following Visual Basic Code plays a Microsoft AVI file at a specified location and size on the active window. This example assumes Microsoft Video for Windows is installed and functioning properly with the BLASTOFF.AVI file stored in the default directory (C:\WINVIDEO). This code does not require the full Microsoft Video package in order to function properly. If the full package is not installed, the runtime module must be properly installed in its place for distribution, and the FileSpec variable must be modified accordingly.

Sample Code:

'API and Constant Declarations
'The function declarations must each be typed on one line.

Declare Function mciSendString Lib "MMSYSTEM" (ByVal lpstrCommand _
   As String, ByVal lpstrReturnStr As Any, ByVal wReturnLen As _
   Any, ByVal hCallBack As Any) As Long

Declare Function GetActiveWindow Lib "USER" () 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:\WINVIDEO\BLASTOFF.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 25 120 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


For more information about the mciSendString function, see the "Microsoft Video for Windows Development Kit", and the Microsoft Visual Basic 3.0 help topic under Win SDK.


Additional query words: 5.00 5.00c 7.00 8.00 97
Keywords : kbprg PgmOthr kbprg
Version : 5.00 5.00c 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.