VBA PowerPoint - VBA-vejledning til oprettelse af Powerpoint-præsentation

Indholdsfortegnelse

Excel VBA PowerPoint

Ved hjælp af VBA kan vi automatisere det arbejde, vi udfører for PowerPoint, men først for at bruge VBA-kode eller uddrag til at arbejde i powerpoint skal du først arbejde gennem sikkerhedsindstillingerne i PowerPoint for at aktivere alle makroer, og derefter kan vi bruge PowerPoint VBA-reference til makroer i MS PowerPoint.

Det smukke ved VBA er, at vi kan henvise til andre Microsoft-produkter som "Microsoft Word" og "Microsoft PowerPoint." Vi opretter normalt rapporter i Excel og opretter derefter PowerPoint-præsentationer. Alle excel-brugere bruger normalt lang tid på at forberede præsentationen ud fra excel-data og rapporter. Hvis du bruger lang tid på at forberede PowerPoint-præsentationer, viser denne tutorial dig, hvordan du opretter en PowerPoint-præsentation fra excel selv ved hjælp af VBA-kodning.

Aktivér Powerpoint-objektmodel

Trin 1: Åbn VBA Editor, og gå derefter til Værktøjer og referencer.

Trin 2: Nu vil du se alle referencerne til VBA-projektet. Rul ned og vælg "Microsoft PowerPoint 15.0 Object Library".

Trin 3: Klik på OK. Nu kan vi få adgang til PowerPoint fra Excel.

VBA-vejledning til oprettelse af PowerPoint-præsentation

Vi kan oprette PPT på to måder ved hjælp af "Early Binding", og en anden bruger "Late Binding." Vi viser dig, hvordan du opretter en PowerPoint-præsentation ved hjælp af "Early Binding" -teknikken .

Normalt forbereder vi fra excel præsentationer baseret på diagrammer og fortolkning af diagrammerne. Så til dette formål har jeg oprettet nogle enkle excel-diagrammer og fortolkninger i det samme regneark.

Trin 1: Start subrutinen i VBA. Nu for at få adgang til PowerPoint har vi allerede aktiveret PowerPoint-objektmodellen i de tidligere trin, nu. For at få adgang til dette skal vi erklære variablen som PowerPoint.Application.

Kode:

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

Trin 2: For at tilføje præsentationen til PowerPoint skal vi erklære en variabel som PowerPoint. Præsentation.

Kode:

 Dim PPPresentation Som PowerPoint.Presentation

Trin 3: Efter at have tilføjet præsentationen til PowerPoint, skal vi tilføje Slide. At erklære variablen som PowerPoint.Slide

Kode:

Dim PPSlide som PowerPoint.Slide

Trin 4: Når diaset er føjet til PowerPoint, er vi nødt til at gøre brug af figurer i PowerPoint, dvs. tekstfelter. At erklære en variabel som PowerPoint.Shape

Kode:

Dim PPShape Som PowerPoint.Shape

Trin 5: For at få adgang til alle diagrammerne i regnearket skal vi erklære variablen som Excel.ChartObjects.

Kode:

Dim PPCharts som Excel.ChartObject

Ok, for at starte proceduren er disse variabler nok.

Trin 6: Nu skal vi starte PowerPoint fra Excel. Da det er et eksternt objekt, er vi nødt til at indstille dette som et nyt PowerPoint.

Kode:

Indstil PPApp = Ny PowerPoint.Application

Dette starter den nye PowerPoint fra excel.

Trin 7: Nu er variablen PPApp lig med den PowerPoint, vi har lanceret. Gør nu denne PowerPoint synlig og maksimer vinduet.

Kode:

PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized

I øjeblikket skal du bare køre koden ved hjælp af F5-tasten eller manuelt. Du skal se PowerPoint-appen lanceret som den nedenfor.

Trin 8: Nu skal vi tilføje en præsentation til PowerPoint-appen, vi har lanceret.

Kode:

Indstil PPPresentation = PPApp.Presentations.Add

Nu skal vi se PowerPoint-præsentationen sådan.

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 artikler...