VBA PowerPoint - VBA-zelfstudie om PowerPoint-presentatie te maken

Inhoudsopgave

Excel VBA PowerPoint

Met behulp van VBA kunnen we het werk dat we doen voor PowerPoint automatiseren, maar om eerst VBA-code of -fragmenten te gebruiken om in PowerPoint te werken, doorloopt u eerst de beveiligingsopties in PowerPoint om alle macro's in te schakelen en vervolgens kunnen we PowerPoint VBA-referentie gebruiken voor macro's in MS Power Point.

Het mooie van VBA is dat we kunnen verwijzen naar andere Microsoft-producten zoals 'Microsoft Word' en 'Microsoft PowerPoint'. We maken meestal rapporten in Excel en maken vervolgens PowerPoint-presentaties. Alle Excel-gebruikers besteden gewoonlijk een aanzienlijke hoeveelheid tijd aan het voorbereiden van de presentatie op basis van Excel-gegevens en -rapporten. Als u veel tijd besteedt aan het voorbereiden van PowerPoint-presentaties, laat deze tutorial u zien hoe u een PowerPoint-presentatie maakt vanuit Excel zelf met behulp van VBA-codering.

Schakel Powerpoint-objectmodel in

Stap 1: Open VBA-editor en ga vervolgens naar Tools en verwijzingen.

Stap 2: Nu ziet u alle verwijzingen naar het VBA-project. Scroll naar beneden en selecteer "Microsoft PowerPoint 15.0 Object Library".

Stap 3: Klik op, Ok. Nu hebben we toegang tot PowerPoint vanuit Excel.

VBA-zelfstudie om PowerPoint-presentatie te maken

We kunnen PPT op twee manieren maken met behulp van 'Early Binding' en een andere met 'Late Binding'. We laten u zien hoe u een PowerPoint-presentatie maakt met behulp van de "Early Binding" -techniek .

Meestal bereiden we vanuit Excel presentaties voor op basis van grafieken en interpretatie van de grafieken. Dus voor dit doel heb ik enkele eenvoudige Excel-grafieken en interpretaties gemaakt in hetzelfde werkblad.

Stap 1: Start de subroutine in VBA. Om toegang te krijgen tot PowerPoint, hebben we het PowerPoint-objectmodel nu al ingeschakeld in de eerdere stappen. Om hier toegang toe te krijgen, moeten we de variabele declareren als PowerPoint.Application.

Code:

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

Stap 2: Om de presentatie aan PowerPoint toe te voegen, moeten we een variabele declareren als PowerPoint.Presentation.

Code:

 Dim PPPresentation As PowerPoint.Presentation

Stap 3: Nadat we de presentatie aan PowerPoint hebben toegevoegd, moeten we Slide toevoegen. Om de variabele aan te geven als PowerPoint.Slide

Code:

Dim PPSlide als PowerPoint.Slide

Stap 4: Zodra de dia aan PowerPoint is toegevoegd, moeten we gebruik maken van vormen in PowerPoint, dwz tekstvakken. Om een ​​variabele aan te geven als PowerPoint.Shape

Code:

Dim PPShape als PowerPoint.Shape

Stap 5: Om toegang te krijgen tot alle grafieken in het werkblad, moeten we de variabele declareren als Excel.ChartObjects.

Code:

Dim PPCharts als Excel.ChartObject

Ok, om de procedure te starten, zijn deze variabelen voldoende.

Stap 6: Nu moeten we PowerPoint starten vanuit Excel. Omdat het een extern object is, moeten we dit instellen als een nieuwe PowerPoint.

Code:

Stel PPApp = New PowerPoint.Application in

Hiermee wordt de nieuwe PowerPoint vanuit Excel gestart.

Stap 7: Nu is de variabele PPApp gelijk aan de PowerPoint die we hebben gelanceerd. Maak nu deze PowerPoint zichtbaar en maximaliseer het venster.

Code:

PPApp.Visible = msoC Waar PPApp.WindowState = ppWindowMaximized

Voer op dit moment de code uit met behulp van de F5-toets of handmatig. U zou de PowerPoint-app moeten zien gelanceerd zoals de onderstaande.

Stap 8: Nu moeten we een presentatie toevoegen aan de PowerPoint-app die we hebben gelanceerd.

Code:

Stel PPPresentation = PPApp.Presentations.Add in

Nu zouden we de PowerPoint-presentatie als volgt moeten zien.

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

Interessante artikelen...