VBA PowerPoint - VBA opplæring for å lage Powerpoint-presentasjon

Innholdsfortegnelse

Excel VBA PowerPoint

Ved å bruke VBA kan vi automatisere arbeidet vi gjør for PowerPoint, men først for å bruke VBA-kode eller utdrag for å jobbe i powerpoint, arbeid først gjennom sikkerhetsalternativene i PowerPoint for å aktivere alle makroer, og deretter kan vi bruke PowerPoint VBA-referanse for makroer i MS PowerPoint.

Det fine med VBA er at vi kan referere til andre Microsoft-produkter som "Microsoft Word" og "Microsoft PowerPoint." Vi oppretter vanligvis rapporter i Excel og lager deretter PowerPoint-presentasjoner. Alle Excel-brukerne bruker vanligvis mye tid på å forberede presentasjonen fra Excel-data og rapporter. Hvis du bruker mye tid på å forberede PowerPoint-presentasjoner, vil denne opplæringen vise deg hvordan du lager en PowerPoint-presentasjon fra å utmerke seg selv ved hjelp av VBA-koding.

Aktiver Powerpoint Object Model

Trinn 1: Åpne VBA Editor og deretter Gå til Verktøy og referanser.

Trinn 2: Nå vil du se alle referansene til VBA-prosjektet. Rull ned og velg "Microsoft PowerPoint 15.0 Object Library".

Trinn 3: Klikk på OK. Nå kan vi få tilgang til PowerPoint fra excel.

VBA opplæring for å lage PowerPoint-presentasjon

Vi kan lage PPT på to måter ved å bruke "Early Binding", og en annen bruker "Late Binding." Vi vil vise deg hvordan du lager en PowerPoint-presentasjon ved hjelp av "Early Binding" -teknikken .

Vanligvis, fra excel, forbereder vi presentasjoner basert på diagrammer og tolkning av diagrammer. Så for dette formålet har jeg laget noen enkle excel-diagrammer og tolkninger i samme regneark.

Trinn 1: Start subrutinen i VBA. Nå for å få tilgang til PowerPoint, har vi allerede aktivert PowerPoint-objektmodellen i de tidligere trinnene, nå. For å få tilgang til dette, må vi erklære variabelen som PowerPoint.Application.

Kode:

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

Trinn 2: For å legge til presentasjonen i PowerPoint, må vi erklære en variabel som PowerPoint. Presentasjon.

Kode:

 Dim PPPresentation Som PowerPoint.Presentation

Trinn 3: Etter at presentasjonen er lagt til PowerPoint, må vi legge til Slide. Å erklære variabelen som PowerPoint.Slide

Kode:

Dim PPSlide Som PowerPoint.Slide

Trinn 4: Når lysbildet er lagt til i PowerPoint, må vi bruke former i PowerPoint, dvs. tekstbokser. Å erklære en variabel som PowerPoint.Shape

Kode:

Dim PPShape Som PowerPoint.Shape

Trinn 5: For å få tilgang til alle diagrammer i regnearket, må vi erklære variabelen som Excel.ChartObjects.

Kode:

Dim PPCharts Som Excel.ChartObject

OK, for å starte saksgangen er disse variablene nok.

Trinn 6: Nå må vi starte PowerPoint fra Excel. Siden det er et eksternt objekt, må vi sette dette som en ny PowerPoint.

Kode:

Sett PPApp = Ny PowerPoint.Application

Dette vil lansere den nye PowerPoint fra excel.

Trinn 7: Nå er variabelen PPApp lik PowerPoint vi har lansert. Gjør nå PowerPoint synlig og maksimer vinduet.

Kode:

PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized

For øyeblikket er det bare å kjøre koden ved hjelp av F5-tasten eller manuelt. Du bør se PowerPoint-appen lansert som den nedenfor.

Trinn 8: Nå må vi legge til en presentasjon i PowerPoint-appen vi har lansert.

Kode:

Sett PPPresentation = PPApp.Presentations.Add

Nå skal vi se PowerPoint-presentasjonen slik.

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