如何使用 Visual Basic .NET 2002 或 Visual Basic .NET 2003 使用自动化创建和显示 PowerPoint 演示文稿

摘要

本文介绍如何使用 Microsoft Visual Basic .NET 2002 或 Visual Basic .NET 2003 使用自动化创建和显示 Microsoft PowerPoint 演示文稿。

更多信息

为 Microsoft PowerPoint 创建自动化客户端

  1. 启动 Microsoft Visual Studio .NET 2002 或 Visual Studio .NET 2003。 在“文件”菜单上,单击“新建”,然后单击“项目”。 从 Visual Basic Projects 类型中选择 Windows 应用程序。 默认情况下会创建 Form1。

  2. 添加对 Microsoft PowerPoint 对象库和 Microsoft Graph 对象库的引用。 为此,请按照下列步骤操作:

    1. 在"项目"菜单上单击"添加引用"。
    2. “COM ”选项卡上,找到 Microsoft PowerPoint 对象库,然后单击 “选择”。 另请找到 Microsoft Graph 对象库,然后单击 “选择”。

    注意 Microsoft Office 2003 及更高版本的 Microsoft Office 包括主要互操作程序集 (PIA) 。 Microsoft Office XP 不包括 PIA,但可能会下载它们。

  3. “添加引用”对话框中单击“确定”以接受所选内容。

  4. “视图 ”菜单上,选择 “工具箱 ”以显示工具箱,并将按钮添加到 Form1。

  5. 双击 Button1。 将显示窗体的代码窗口。

  6. 在代码窗口中,找到以下代码

        Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    
    End Sub
    
    

    替换为以下代码:

        Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    
    Const sTemplate = _
               "C:\Program Files\Microsoft Office\Templates\Presentation Designs\Blends.pot"
            Const sPic = "C:\WINNT\Soap Bubbles.bmp"
    
    Dim oApp As PowerPoint.Application
            Dim oPres As PowerPoint.Presentation
            Dim oSlide As PowerPoint.Slide
            Dim bAssistantOn As Boolean
    
    'Start Powerpoint and make its window visible but minimized.
            oApp = New PowerPoint.Application()
            oApp.Visible = True
            oApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized
    
    'Create a new presentation based on the specified template.
            oPres = oApp.Presentations.Open(sTemplate, , , True)
    
    'Build Slide #1:
            'Add text to the slide, change the font and insert/position a 
            'picture on the first slide.
            oSlide = oPres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            With oSlide.Shapes.Item(1).TextFrame.TextRange
                .Text = "My Sample Presentation"
                .Font.Name = "Comic Sans MS"
                .Font.Size = 48
            End With
            oSlide.Shapes.AddPicture(sPic, False, True, 150, 150, 500, 350)
            oSlide = Nothing
    
    'Build Slide #2:
            'Add text to the slide title, format the text. Also add a chart to the
            'slide and change the chart type to a 3D pie chart.
            oSlide = oPres.Slides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            With oSlide.Shapes.Item(1).TextFrame.TextRange
                .Text = "My Chart"
                .Font.Name = "Comic Sans MS"
                .Font.Size = 48
            End With
            Dim oChart As Graph.Chart
            oChart = oSlide.Shapes.AddOLEObject(150, 150, 480, 320, _
                        "MSGraph.Chart.8").OLEFormat.Object
            oChart.ChartType = Graph.XlChartType.xl3DPie
            oChart = Nothing
            oSlide = Nothing
    
    'Build Slide #3:
            'Add a text effect to the slide and apply shadows to the text effect.
            oSlide = oPres.Slides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank)
            oSlide.FollowMasterBackground = False
            Dim oShape As PowerPoint.Shape
            oShape = oSlide.Shapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect27, _
                "The End", "Impact", 96, False, False, 230, 200)
            oShape.Shadow.ForeColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppForeground
            oShape.Shadow.Visible = True
            oShape.Shadow.OffsetX = 3
            oShape.Shadow.OffsetY = 3
            oShape = Nothing
            oSlide = Nothing
    
    'Modify the slide show transition settings for all 3 slides in
            'the presentation.
            Dim SlideIdx(3) As Integer
            SlideIdx(0) = 1
            SlideIdx(1) = 2
            SlideIdx(2) = 3
            With oPres.Slides.Range(SlideIdx).SlideShowTransition
                .AdvanceOnTime = True
                .AdvanceTime = 3
                .EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut
            End With
            Dim oSettings As PowerPoint.SlideShowSettings
            oSettings = oPres.SlideShowSettings
            oSettings.StartingSlide = 1
            oSettings.EndingSlide = 3
    
    'Prevent Office Assistant from displaying alert messages.
            bAssistantOn = oApp.Assistant.On
            oApp.Assistant.On = False
    
    'Run the slide show and wait for the slide show to end.
            oSettings.Run()
            Do While oApp.SlideShowWindows.Count >= 1
                System.Windows.Forms.Application.DoEvents()
            Loop
            oSettings = Nothing
    
    'Reenable Office Assisant, if it was on.
            If bAssistantOn Then
                oApp.Assistant.On = True
                oApp.Assistant.Visible = False
            End If
    
    'Close the presentation without saving changes and quit PowerPoint.
            oPres.Saved = True
            oPres.Close()
            oPres = Nothing
            oApp.Quit()
            oApp = Nothing
            GC.Collect()
        End Sub
    
    

    注意 在此代码中,sTemplate 和 sPic 常量分别表示 PowerPoint 模板和图片的完整路径和文件名。 根据需要修改这些路径,以使用系统上安装的模板或图片。

  7. 将以下代码添加到 Form1.vb 的顶部:

    Imports Office = Microsoft.Office.Core
    Imports Graph = Microsoft.Office.Interop.Graph
    Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
    
  8. 按 F5 生成,然后运行程序。

  9. 单击窗体上的 Button1 创建,然后显示 PowerPoint 演示文稿。

参考

有关详细信息,请参阅以下 Microsoft 开发人员网络 (MSDN) 网站:Microsoft Office System 2003 培训Visual Studio Tools