Comment utiliser l’automatisation pour créer et afficher une présentation PowerPoint à l’aide de Visual Basic .NET 2002 ou Visual Basic .NET 2003

Résumé

Cet article explique comment utiliser l’automatisation pour créer et afficher une présentation Microsoft PowerPoint à l’aide de Microsoft Visual Basic .NET 2002 ou Visual Basic .NET 2003.

Informations supplémentaires

Créer un client Automation pour Microsoft PowerPoint

  1. Démarrez Microsoft Visual Studio .NET 2002 ou Visual Studio .NET 2003. Dans le menu Fichier, cliquez sur Nouveau, puis sur Projet. Sélectionnez Application Windows dans les types de projets Visual Basic. Form1 est créé par défaut.

  2. Ajoutez une référence à la bibliothèque d’objets Microsoft PowerPoint et à la bibliothèque d’objets Microsoft Graph. Pour cela, procédez comme suit :

    1. Dans le menu Projet, cliquez sur Ajouter une référence.
    2. Sous l’onglet COM , recherchez la bibliothèque d’objets Microsoft PowerPoint, puis cliquez sur Sélectionner. Recherchez également la bibliothèque d’objets Microsoft Graph, puis cliquez sur Sélectionner.

    Note Microsoft Office 2003 et les versions ultérieures de Microsoft Office incluent des assemblys d’interopérabilité principal (PIA). Microsoft Office XP n’inclut pas de codes confidentiels, mais ils peuvent être téléchargés.

  3. Cliquez sur OK dans la boîte de dialogue Ajouter des références pour accepter vos sélections.

  4. Dans le menu Affichage , sélectionnez Boîte à outils pour afficher la boîte à outils et ajouter un bouton à Form1.

  5. Double-cliquez sur Button1. La fenêtre de code du formulaire s’affiche.

  6. Dans la fenêtre de code, recherchez le code suivant

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

    Remplacez par le code suivant :

        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
    
    

    Note Dans ce code, les constantes sTemplate et sPic représentent le chemin d’accès complet et le nom de fichier d’un modèle PowerPoint et d’une image, respectivement. Modifiez ces chemins d’accès en fonction des besoins pour utiliser un modèle ou une image installé sur votre système.

  7. Ajoutez le code suivant en haut de Form1.vb :

    Imports Office = Microsoft.Office.Core
    Imports Graph = Microsoft.Office.Interop.Graph
    Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
    
  8. Appuyez sur F5 pour générer, puis exécutez le programme.

  9. Cliquez sur Button1 sur le formulaire à créer, puis affichez une présentation PowerPoint.

References

Pour plus d’informations, consultez le site web Microsoft Developer Network (MSDN) suivant : Visual Studio Tools pour la formation Microsoft Office System 2003