你目前正处于脱机状态,正在等待 Internet 重新连接

如何使用 Visual Basic .NET 2003 处理 Office PowerPoint 2003 事件

针对 Office 2003 的支持已终止

Microsoft 已于 2014 年 4 月 8 日终止了针对 Office 2003 的支持。该更改已影响到您的软件更新和安全选项。 了解这一措施对于您的含义以及如何继续保持受保护状态。

简介
本文介绍如何从使用 Microsoft Visual Basic .NET 2003 开发的自动化客户端处理 Microsoft Office PowerPoint 2003 事件。

返回页首

构造事件处理程序

可以在 Visual Basic .NET 中使用以下任一方法来构造事件处理程序。具体选择哪种方法取决于您希望如何关联事件处理程序和事件。
  • 方法 1

    通常使用 Handles 关键字和 WithEvents 关键字创建事件处理程序。使用 WithEvents 关键字声明对象引用变量后,Visual Basic .NET 会自动在运行时连接到对象的事件。要处理对象的特定事件,应在代码视图中使用 Visual Studio .NET 环境的列表和方法列表来添加相关的处理程序。
  • 方法 2

    Visual Basic .NET 提供了另一种处理事件的方法。您可以使用 AddHandler 语句和 RemoveHandler 语句,这样就可以动态启动和停止特定事件的事件处理过程。

    注意:如果使用 WithEvents 关键字构造事件处理程序,可能会收到 System.Reflection.TargetInvocationException 异常。因此,应使用 AddHandler 事件来构造事件处理程序。
返回页首

创建用于处理 PowerPoint 事件的 Visual Basic .NET 自动化客户端

下面几组步骤演示了如何使用 AddHandler 语句从使用 Visual Basic .NET 开发的自动化客户端来处理 PowerPoint 应用程序 事件。

要创建 Visual Basic .NET 项目,请按照下列步骤操作:
  1. 启动 Microsoft Visual Studio .NET 2003。
  2. 在“文件”菜单上,单击“新建”,然后单击“项目”。
  3. 在“项目类型”列表中,单击“Visual Basic 项目”。在“模板”列表中,单击“Windows 应用程序”。
  4. 将项目命名为 PowerPointEvents,然后单击“确定”。

    默认情况下,创建一个名为 Form1 的表单。
要添加对 Microsoft PowerPoint 对象库和 Microsoft Graph 对象库的引用,请按照下列步骤操作:
  1. 在“项目”菜单上,单击“添加引用”。
  2. 在“COM”选项卡上,单击“Microsoft PowerPoint 11.0 对象库”,然后单击“选择”。
  3. 在“COM”选项卡上,单击“Microsoft Graph 11.0 对象库”,然后单击“选择”。
  4. 在“添加引用”对话框中,单击“确定”以接受您的选择。
  5. 双击该表单。

    此时将打开“代码”窗口。
  6. 在 Form1.vb 文件的顶部,添加下面的代码:
    	Imports PowerPoint = Microsoft.Office.Interop.PowerPoint	Imports Office = Microsoft.Office.Core	Imports Graph = Microsoft.Office.Interop.Graph
  7. 在“视图”菜单上,单击“设计器”。
  8. 在“视图”菜单上,单击“工具箱”,然后向 Form1 添加一个按钮。
  9. 双击“Button1”。

    “代码”窗口打开,显示 Button1_Click 事件。
  10. 在“代码”窗口中,将下面的代码放在 Button1_Click 事件之前:
    	Dim oApp As PowerPoint.Application	Dim oPres As PowerPoint.Presentation
  11. 将下面的代码添加到 Button1_Click 事件中:
    	Const sTemplate = "C:\Program Files\Microsoft Office\Templates\Presentation Designs\Orbit.pot"        ' If the previous template is not available, change the template name to a template name that is on your system.        Const sVideo = "C:\WINDOWS\system32\oobe\images\intro.wmv"        Dim oPresentations As PowerPoint.Presentations        Dim oSlides As PowerPoint.Slides        Dim oSlide As PowerPoint.Slide        Dim oShapes As PowerPoint.Shapes        Dim oShape As PowerPoint.Shape        Dim oMovie As PowerPoint.Shape        Dim oAnimationSettings As PowerPoint.AnimationSettings        Dim oPlaySettings As PowerPoint.PlaySettings        Dim oTextFrame As PowerPoint.TextFrame        Dim oTextRange As PowerPoint.TextRange        Dim oFont As PowerPoint.Font        Dim oOLEFormat As PowerPoint.OLEFormat        Dim oShadow As PowerPoint.ShadowFormat        Dim oForeColor As PowerPoint.ColorFormat        Dim oRange As PowerPoint.SlideRange        Dim oSlideShowTransition As PowerPoint.SlideShowTransition        'Start PowerPoint and then make the PowerPoint window visible but minimized.        oApp = New PowerPoint.Application        'Add event handlers.        AddHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin        AddHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide        AddHandler oApp.PresentationClose, AddressOf oApp_PresentationClose        oApp.Visible = True        oApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized        'Create a new presentation that is based on the specified template.        oPresentations = oApp.Presentations        oPres = oPresentations.Open(sTemplate, , , True)        'Build slide 1.        'Add text to the slide, change the font, and then insert or position a         'movie on the first slide.        oSlides = oPres.Slides        oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)        oShapes = oSlide.Shapes        oShape = oShapes.Item(1)        oTextFrame = oShape.TextFrame        oTextRange = oTextFrame.TextRange        oTextRange.Text = "My sample presentation"        oFont = oTextRange.Font        oFont.Name = "Comic Sans MS"        oFont.Size = 48        oMovie = oShapes.AddMediaObject(sVideo, 150, 150, 500, 350)        oAnimationSettings = oMovie.AnimationSettings        oPlaySettings = oAnimationSettings.PlaySettings        oPlaySettings.PlayOnEntry = True        oPlaySettings.HideWhileNotPlaying = True        'NAR to release references.        NAR(oPlaySettings)        NAR(oAnimationSettings)        NAR(oMovie)        NAR(oFont)        NAR(oTextRange)        NAR(oTextFrame)        NAR(oShape)        NAR(oShapes)        NAR(oSlide)        NAR(oSlides)        'Build slide 2.        'Add text to the slide title and then format the text. Also, add a chart to the        'slide and then change the chart type to a 3D Column Clustered.        oSlides = oPres.Slides        oSlide = oSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)        oShapes = oSlide.Shapes        oShape = oShapes.Item(1)        oTextFrame = oShape.TextFrame        oTextRange = oTextFrame.TextRange        oTextRange.Text = "My chart"        oFont = oTextRange.Font        oFont.Name = "Comic Sans MS"        oFont.Size = 48        Dim oChart As Graph.Chart        oShape = oShapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8")        oOLEFormat = oShape.OLEFormat        oChart = oOLEFormat.Object        oChart.ChartType = Graph.XlChartType.xl3DColumnClustered        'NAR to release references.        NAR(oChart)        NAR(oOLEFormat)        NAR(oFont)        NAR(oTextRange)        NAR(oTextFrame)        NAR(oShape)        NAR(oShapes)        NAR(oSlide)        NAR(oSlides)        'Build slide 3.        'Add a text effect to the slide and then apply shadows to the text effect.        oSlides = oPres.Slides        oSlide = oSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank)        oSlide.FollowMasterBackground = False        oShapes = oSlide.Shapes        oShape = oShapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect27, _            "The End", "Impact", 96, False, False, 230, 200)        oShadow = oShape.Shadow        oForeColor = oShadow.ForeColor        oForeColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppForeground        oShadow.Visible = True        oShadow.OffsetX = 3        oShadow.OffsetY = 3        'NAR to release references.        NAR(oShadow)        NAR(oForeColor)        NAR(oShape)        NAR(oShapes)        NAR(oSlide)        NAR(oSlides)        'Modify the slide show transition settings for all three slides in        'the presentation.        Dim SlideIdx(3) As Integer        SlideIdx(0) = 1        SlideIdx(1) = 2        SlideIdx(2) = 3        oSlides = oPres.Slides        oRange = oSlides.Range(SlideIdx)        oSlideShowTransition = oRange.SlideShowTransition        oSlideShowTransition.AdvanceOnTime = False        oSlideShowTransition.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut        'Run the slide show in a separate method.        RunSlideShow()        'NAR to release references.        NAR(oSlideShowTransition)        NAR(oRange)        NAR(oSlides)        'Close the presentation without saving changes and then quit PowerPoint.        oPres.Saved = True        oPres.Close()        'NAR the presentation objects.        NAR(oPres)        NAR(oPresentations)        'Remove all event handlers.        RemoveHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin        RemoveHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide        RemoveHandler oApp.PresentationClose, AddressOf oApp_PresentationClose        'Quit PowerPoint.        oApp.Quit()        NAR(oApp)        GC.Collect()
  12. 在“代码”窗口中,将下面的代码放在 Button1_Click 事件的下面:
    	Private Sub RunSlideShow()            Dim oSettings As PowerPoint.SlideShowSettings	    Dim oSlideShowWindows As PowerPoint.SlideShowWindows	    oSettings = oPres.SlideShowSettings	    oSettings.StartingSlide = 1       	    oSettings.EndingSlide = 3            'Run the slide show. Wait for the slide show to finish.            oSettings.Run()            oSlideShowWindows = oApp.SlideShowWindows            On Error Resume Next            Do While oSlideShowWindows.Count >= 1	        System.Windows.Forms.Application.DoEvents()       	    Loop	    NAR(oSlideShowWindows)            NAR(oSettings)	End Sub    	'NAR function to remove reference.   	Private Sub NAR(ByVal o As Object)            Try	        System.Runtime.InteropServices.Marshal.ReleaseComObject(o)            Catch            Finally                o = Nothing            End Try    	End Sub
  13. 将下面的事件处理程序添加在上一段代码之后:
    	'Event Handlers    	Private Sub oApp_SlideShowBegin(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)            'Change the position and the size of the slide show window.            Dim oView As PowerPoint.SlideShowView            With Wn                .Height = 325                .Width = 400                .Left = 100                .Activate()            End With           'Start the movie at the first slide.            oView = Wn.View            oView.Next()            NAR(oView)            NAR(Wn)    	End Sub    	Private Sub oApp_SlideShowNextSlide(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)            'Change the color and the type of pointer depending on the slide.            Dim Showpos As Integer            Dim oView As PowerPoint.SlideShowView            Dim oColorFormat As PowerPoint.ColorFormat            oView = Wn.View            Showpos = oView.CurrentShowPosition + 1            If Showpos = 3 Then                oColorFormat = oView.PointerColor                oColorFormat.RGB = RGB(255, 0, 0)                oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerPen            Else                oColorFormat = oView.PointerColor                oColorFormat.RGB = RGB(0, 0, 0)                oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerArrow            End If            NAR(oColorFormat)            NAR(oView)            NAR(Wn)    	End Sub    	Private Sub oApp_PresentationClose(ByVal Pres As Microsoft.Office.Interop.PowerPoint.Presentation)            'Before you close the presentation, save the presentation as HTML.            Pres.SaveAs("C:\TestEvents.htm", PowerPoint.PpSaveAsFileType.ppSaveAsHTML)            NAR(Pres)    	End Sub
返回页首

试运行

  1. 按 F5 键运行该应用程序。
  2. 单击“Button1”

    PowerPoint 开始放映幻灯片。您将看到:
    • 第一张幻灯片启动一个影片。
    • 指针类型发生变化。
    • 第二张带有图表的幻灯片的颜色发生变化。
  3. 关闭该演示文稿。
返回页首
参考
有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
308330如何用 Visual Basic .NET 2002 处理 PowerPoint 2002 事件
254009 可供下载的 PowerPoint 2000 事件演示
303717 如何通过使用 Visual Basic .NET 2002 或 Visual Basic .NET 2003 来使用自动化创建和显示 PowerPoint 2002 演示文稿或 Office PowerPoint 2003 演示文稿
823996 在 Visual Studio .NET 中引用 Office 类型库时找不到 PIA
返回页首
powerpoint vb net event power point connection
属性

文章 ID:824021 - 上次审阅时间:10/12/2005 12:00:00 - 修订版本: 1.0

Microsoft Office PowerPoint 2003, Microsoft Visual Basic .NET 2003 标准版

  • kbhowtomaster kbautomation KB824021
反馈