PowerPoint de VBA - Tutorial de VBA para crear una presentación de PowerPoint

Tabla de contenido

Excel VBA PowerPoint

Usando VBA podemos automatizar el trabajo que hacemos para PowerPoint, pero primero para usar código VBA o fragmentos para trabajar en powerpoint, primero trabaje a través de las opciones de seguridad en PowerPoint para habilitar todas las macros y luego podemos usar la referencia de PowerPoint VBA para macros en MS PowerPoint.

La belleza de VBA es que podemos hacer referencia a otros productos de Microsoft como "Microsoft Word" y "Microsoft PowerPoint". Por lo general, creamos informes en Excel y luego creamos presentaciones de PowerPoint. Todos los usuarios de Excel suelen dedicar una cantidad considerable de tiempo a preparar la presentación a partir de datos e informes de Excel. Si está dedicando una cantidad considerable de tiempo a preparar presentaciones de PowerPoint, este tutorial le mostrará cómo crear una presentación de PowerPoint desde Excel usando la codificación VBA.

Habilitar el modelo de objetos de Powerpoint

Paso 1: Abra VBA Editor y luego vaya a Herramientas y referencias.

Paso 2: Ahora, verá todas las referencias al proyecto VBA. Desplácese hacia abajo y seleccione "Biblioteca de objetos de Microsoft PowerPoint 15.0".

Paso 3: Haga clic en Aceptar. Ahora podemos acceder a PowerPoint desde Excel.

Tutorial de VBA para crear una presentación de PowerPoint

Podemos crear PPT de dos formas, una mediante "Enlace anticipado" y otra mediante "Enlace tardío". Le mostraremos cómo crear una presentación de PowerPoint utilizando la técnica de "Encuadernación anticipada" .

Por lo general, desde Excel, preparamos presentaciones basadas en gráficos e interpretación de los gráficos. Entonces, para este propósito, he creado algunos gráficos e interpretaciones de Excel simples en la misma hoja de trabajo.

Paso 1: Inicie la subrutina en VBA. Ahora para acceder a PowerPoint, ya hemos habilitado el modelo de objetos de PowerPoint en los pasos anteriores, ahora. Para acceder a esto, necesitamos declarar la variable como PowerPoint.Application.

Código:

Sub PPT_Example () Dim PPApp como PowerPoint.Application End Sub

Paso 2: Para agregar la presentación a PowerPoint, necesitamos declarar una variable como PowerPoint.Presentation.

Código:

 Dim PPPresentation como presentación de PowerPoint.

Paso 3: Después de agregar la presentación a PowerPoint, debemos agregar la diapositiva. Para declarar la variable como PowerPoint.

Código:

Atenuar PPSlide como PowerPoint.

Paso 4: Una vez que se agrega la diapositiva al PowerPoint, necesitamos hacer uso de formas en el PowerPoint, es decir, cuadros de texto. Para declarar una variable como PowerPoint.

Código:

Atenuar PPShape como PowerPoint.

Paso 5: Ahora, para acceder a todos los gráficos en la hoja de trabajo, necesitamos declarar la variable como Excel.ChartObjects.

Código:

Atenuar PPCharts como Excel.ChartObject

Ok, para empezar el trámite, estas variables son suficientes.

Paso 6: Ahora, necesitamos iniciar PowerPoint desde Excel. Dado que es un objeto externo, debemos configurarlo como un nuevo PowerPoint.

Código:

Establecer PPApp = Nueva aplicación de PowerPoint.

Esto lanzará el nuevo PowerPoint desde Excel.

Paso 7: Ahora, la variable PPApp es igual al PowerPoint que hemos lanzado. Ahora haga visible este PowerPoint y maximice la ventana.

Código:

PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized

En este momento, simplemente ejecute el código usando la tecla F5 o manualmente. Debería ver la aplicación de PowerPoint iniciada como la siguiente.

Paso 8: Ahora, debemos agregar una presentación a la aplicación de PowerPoint que hemos lanzado.

Código:

Establecer PPPresentation = PPApp.Presentations.Add

Ahora deberíamos ver la presentación de PowerPoint así.

Step 9: After adding the presentation, we need to add a slide.

Code:

Set PPSlide = PPPresentation.Slides.Add(1, ppLayoutTitleOnly)

Now this will add the title slide like the below.

Step 10: Now we have more than one chart in the worksheet, we need to loop through each chart and paste in the presentation. Below is the code to copy and paste the chart as well as interpretation.

Below is the complete code for you.

Sub PPT_Example() Dim PPApp As PowerPoint.Application Dim PPPresentation As PowerPoint.Presentation Dim PPSlide As PowerPoint.Slide Dim PPShape As PowerPoint.Shape Dim PPCharts As Excel.ChartObject Set PPApp = New PowerPoint.Application PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized 'Add Presentation Set PPPresentation = PPApp.Presentations.Add 'Loop through each chart in the Excel and paste into the PowerPoint For Each PPCharts In ActiveSheet.ChartObjects PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutText PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count Set PPSlide = PPApp.ActivePresentation.Slides(PPApp.ActivePresentation.Slides.Count) 'Copy the chart and paste in Powerpoint PPCharts.Select ActiveChart.ChartArea.Copy PPSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select 'Add heading to the slide PPSlide.Shapes(1).TextFrame.TextRange.Text = PPCharts.Chart.ChartTitle.Text 'Allignment of the chart PPApp.ActiveWindow.Selection.ShapeRange.Left = 15 PPApp.ActiveWindow.Selection.ShapeRange.Top = 125 PPSlide.Shapes(2).Width = 200 PPSlide.Shapes(2).Left = 505 'Add interpretation If InStr(PPSlide.Shapes(1).TextFrame.TextRange.Text, "Region") Then PPSlide.Shapes(2).TextFrame.TextRange.Text = Range("K2").Value & vbNewLine PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K3").Value & vbNewLine) 'Else if the chart is the "Renewable" consumption chart, then enter the appropriate comments ElseIf InStr(PPSlide.Shapes(1).TextFrame.TextRange.Text, "Month") Then PPSlide.Shapes(2).TextFrame.TextRange.Text = Range("K20").Value & vbNewLine PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K21").Value & vbNewLine) PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K22").Value & vbNewLine) End If 'Now let's change the font size of the callouts box PPSlide.Shapes(2).TextFrame.TextRange.Font.Size = 16 Next PPCharts End Sub

Articulos interesantes...