PowerPoint VBA - Esercitazione VBA per creare presentazioni PowerPoint

Excel VBA PowerPoint

Usando VBA possiamo automatizzare il lavoro che facciamo per PowerPoint, ma prima di utilizzare il codice VBA o gli snippet per lavorare in PowerPoint prima lavoriamo attraverso le opzioni di sicurezza in PowerPoint per abilitare tutte le macro e poi possiamo usare il riferimento VBA di PowerPoint per le macro in MS Presa della corrente.

Il bello di VBA è che possiamo fare riferimento ad altri prodotti Microsoft come "Microsoft Word" e "Microsoft PowerPoint". Di solito creiamo report in Excel e quindi creiamo presentazioni PowerPoint. Tutti gli utenti di Excel di solito impiegano una notevole quantità di tempo per preparare la presentazione dai dati e dai rapporti di Excel. Se trascorri una notevole quantità di tempo nella preparazione di presentazioni PowerPoint, questo tutorial ti mostrerà come creare una presentazione PowerPoint da Excel stesso utilizzando la codifica VBA.

Abilita il modello a oggetti di PowerPoint

Passaggio 1: apri l'editor VBA e quindi vai su Strumenti e riferimenti.

Passaggio 2: ora vedrai tutti i riferimenti al progetto VBA. Scorri verso il basso e seleziona "Libreria oggetti di Microsoft PowerPoint 15.0".

Passaggio 3: fare clic su, Ok. Ora possiamo accedere a PowerPoint da Excel.

Esercitazione VBA per creare presentazioni PowerPoint

Possiamo creare PPT in due modi: uno utilizzando "Early Binding" e un altro utilizzando "Late Binding. Ti mostreremo come creare una presentazione PowerPoint utilizzando la tecnica "Early Binding" .

Di solito, da Excel, prepariamo presentazioni basate su grafici e interpretazione dei grafici. Quindi, per questo scopo, ho creato alcuni semplici grafici e interpretazioni Excel nello stesso foglio di lavoro.

Passaggio 1: avviare la subroutine in VBA. Ora per accedere a PowerPoint, abbiamo già abilitato il modello a oggetti di PowerPoint nei passaggi precedenti, ora. Per accedervi, dobbiamo dichiarare la variabile come PowerPoint.Application.

Codice:

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

Passaggio 2: per aggiungere la presentazione a PowerPoint, è necessario dichiarare una variabile come PowerPoint.Presentation.

Codice:

 Dim PPPresentation come PowerPoint.Presentation

Passaggio 3: dopo aver aggiunto la presentazione a PowerPoint, è necessario aggiungere Slide. Per dichiarare la variabile come PowerPoint.Slide

Codice:

Dim PPSlide As PowerPoint.Slide

Passaggio 4: una volta aggiunta la diapositiva a PowerPoint, è necessario utilizzare le forme in PowerPoint, ovvero le caselle di testo. Per dichiarare una variabile come PowerPoint.Shape

Codice:

Dim PPShape come PowerPoint.Shape

Passaggio 5: ora, per accedere a tutti i grafici nel foglio di lavoro, è necessario dichiarare la variabile come Excel.ChartObjects.

Codice:

Dim PPCharts As Excel.ChartObject

Ok, per iniziare il procedimento, bastano queste variabili.

Passaggio 6: ora, dobbiamo avviare PowerPoint da Excel. Poiché si tratta di un oggetto esterno, è necessario impostarlo come nuovo PowerPoint.

Codice:

Imposta PPApp = New PowerPoint.Application

Questo lancerà il nuovo PowerPoint da Excel.

Passaggio 7: ora, la variabile PPApp è uguale al PowerPoint che abbiamo lanciato. Ora rendi visibile questo PowerPoint e ingrandisci la finestra.

Codice:

PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized

In questo momento, esegui il codice usando il tasto F5 o manualmente. Dovresti vedere l'app PowerPoint lanciata come quella sotto.

Passaggio 8: ora è necessario aggiungere una presentazione all'app PowerPoint che abbiamo avviato.

Codice:

Imposta PPPresentation = PPApp.Presentations.Add

Ora dovremmo vedere la presentazione di PowerPoint in questo modo.

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

Articoli interessanti...