Compartir a través de


Cómo usar la automatización para crear y mostrar una presentación de PowerPoint mediante Visual Basic .NET 2002 o Visual Basic .NET 2003

Resumen

En este artículo se describe cómo usar la automatización para crear y mostrar una presentación de Microsoft PowerPoint mediante Microsoft Visual Basic .NET 2002 o Visual Basic .NET 2003.

Más información

Creación de un cliente de automatización para Microsoft PowerPoint

  1. Inicie Microsoft Visual Studio .NET 2002 o Visual Studio .NET 2003. En el menú Archivo, haga clic en Nuevo y, a continuación, haga clic en Proyecto. Seleccione Aplicación windows en los tipos de proyectos de Visual Basic. Form1 se crea de forma predeterminada.

  2. Agregue una referencia a la biblioteca de objetos de Microsoft PowerPoint y a la biblioteca de objetos de Microsoft Graph. Para ello, siga estos pasos:

    1. On the Project menu, click Add Reference.
    2. En la pestaña COM , busque la Biblioteca de objetos de Microsoft PowerPoint y, a continuación, haga clic en Seleccionar. Busque también la biblioteca de objetos de Microsoft Graph y, a continuación, haga clic en Seleccionar.

    Nota Microsoft Office 2003 y versiones posteriores de Microsoft Office incluyen ensamblados de interoperabilidad primarios (PIA). Microsoft Office XP no incluye los PIA, pero se pueden descargar.

  3. Haga clic en Aceptar en el cuadro de diálogo Agregar referencias para aceptar las selecciones.

  4. En el menú Ver , seleccione Cuadro de herramientas para mostrar el cuadro de herramientas y agregar un botón a Form1.

  5. Haga doble clic en Button1. Aparece la ventana de código del formulario.

  6. En la ventana de código, busque el código siguiente.

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

    Reemplace por el código siguiente:

        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
    
    

    Nota En este código, las constantes sTemplate y sPic representan la ruta de acceso completa y el nombre de archivo de una plantilla de PowerPoint y una imagen, respectivamente. Modifique estas rutas de acceso según sea necesario para usar una plantilla o imagen instalada en el sistema.

  7. Agregue el código siguiente a la parte superior de Form1.vb:

    Imports Office = Microsoft.Office.Core
    Imports Graph = Microsoft.Office.Interop.Graph
    Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
    
  8. Presione F5 para compilar y, a continuación, ejecutar el programa.

  9. Haga clic en Button1 en el formulario para crear y, a continuación, mostrar una presentación de PowerPoint.

Referencias

Para obtener más información, consulte el siguiente sitio web de Microsoft Developer Network (MSDN): Visual Studio Tools para el entrenamiento de Microsoft Office System 2003.