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 プロジェクトの種類から 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. [参照の追加] ダイアログ ボックスで [OK] をクリックして、選択内容を受け入れます。

  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 Developer Network (MSDN) Web サイト:Microsoft Office System 2003 トレーニングのVisual Studio Toolsを参照してください。