此示例演示如何以编程方式使用Microsoft Office 2010中的Microsoft Visual Basic for Applications(VBA)创建视频。
此代码段是Office 2010的101项VBA代码示例中的一部分。与其它示例一样,这些将可以直接写入您的代码中。
每块示例代码包含约5至50行的代码,分别演示了一个独特的功能或功能集,在VBA或VB以及C#中(在Visual Studio 2010中创建)。每个示例之中都会包含代码以及相应注释,这样您就可以直接运行获取预期的结果,或者是根据代码注释提示来调整环境,运行示例代码。
Microsoft Office 2010提供了你所需要的工具来创建功能强大的应用程序。Microsoft Visual Basic Application(VBA)代码示例可以帮助你创建自己的应用程序,以执行特定功能或者以此为出发点实现更为复杂的功能。
实例代码
将此代码放在PowerPoint 2010演示文稿一个模块内并测试运行。
' PowerPoint 2010
' Place this code in a module within a PowerPoint 2010 presentation,
' and run the following test procedure. Modify the output file name
' as necessary:
Sub TestCreateSampleVideo()
CreateSampleVideo ActivePresentation, "C:\Temp\Video.wmv"
End Sub
Sub CreateSampleVideo(pres As Presentation, fileName As String)
' Presentation.CreateVideo does its work asynchronously.
' You can use the Presentation.CreateVideoStatus property
' to periodically check the status, and react accordingly.
' Besides the file name, the CreateVideo method accepts the following
' parameters:
' UseTimingsAndNarration indicates whether to use the presentation's
' timings and narrations that you have supplied. If false, the
' conversion disregards this information. The default is True.
' DefaultSlideDuration indicates the default timing for each slide,
' if you have haven't specified a timing or if you set
' UseTimingsAndNarration to false. The default value is 5 seconds.
' VertResolution indicates the vertical resolution for your movie. The
' default is 720. Regular options include 720, 480, and 240, although you
' can specify any reasonable value you like (200 would work, for example,
' although it's not a standard vertical resolution.)
' FramesPerSecond indicates the number of frames per second in the
' output video. The default value is 30, and unless you have a reason
' to change this, leave it alone.
' Quality indicates a relative quality of the video, and the default
' value is 85. The larger the number, the larger the output and the longer
' it takes to create the video. Unless you have a reason, leave this
' at the default value. Try setting the value to a low number: You'll see
' a definite degradation in output quality.
pres.CreateVideo fileName, DefaultSlideDuration:=1, VertResolution:=480
' Now wait for the conversion to be complete:
Do
' Don't tie up the user interface, add DoEvents
' to give the mouse and keyboard time to keep up.
DoEvents
Select Case pres.CreateVideoStatus
Case PpMediaTaskStatus.ppMediaTaskStatusDone
MsgBox "Conversion complete!"
Exit Do
Case PpMediaTaskStatus.ppMediaTaskStatusFailed
MsgBox "Conversion failed!"
Exit Do
Case PpMediaTaskStatus.ppMediaTaskStatusInProgress
Debug.Print "Conversion in progress"
Case PpMediaTaskStatus.ppMediaTaskStatusNone
' This shouldn't happen--you'll get this value
' when you ask for the status and no conversion
' is happening or has completed.
Case PpMediaTaskStatus.ppMediaTaskStatusQueued
Debug.Print "Conversion queued"
End Select
Loop
End Sub