VBA PowerPoint - VBA-handledning för att skapa Powerpoint-presentation

Innehållsförteckning

Excel VBA PowerPoint

Med VBA kan vi automatisera det arbete vi gör för PowerPoint, men först för att kunna använda VBA-kod eller utdrag för att arbeta i powerpoint ska du först arbeta igenom säkerhetsalternativen i PowerPoint för att aktivera alla makron och sedan kan vi använda PowerPoint VBA-referens för makron i MS PowerPoint.

Skönheten i VBA är att vi kan referera till andra Microsoft-produkter som "Microsoft Word" och "Microsoft PowerPoint." Vi skapar vanligtvis rapporter i Excel och skapar sedan PowerPoint-presentationer. Alla Excel-användare spenderar vanligtvis mycket tid på att förbereda presentationen utifrån Excel-data och rapporter. Om du spenderar mycket tid på att förbereda PowerPoint-presentationer, kommer den här guiden att visa dig hur du skapar en PowerPoint-presentation från att utmärka sig med hjälp av VBA-kodning.

Aktivera Powerpoint Object Model

Steg 1: Öppna VBA Editor och gå sedan till Verktyg och referenser.

Steg 2: Nu ser du alla referenser till VBA-projektet. Rulla ner och välj “Microsoft PowerPoint 15.0 Object Library”.

Steg 3: Klicka på OK. Nu kan vi komma åt PowerPoint från Excel.

VBA-handledning för att skapa PowerPoint-presentation

Vi kan skapa PPT på två sätt genom att använda "Early Binding" och en annan med "Late Binding". Vi kommer att visa dig hur du skapar en PowerPoint-presentation med "Early Binding" -tekniken .

Från Excel förbereder vi vanligtvis presentationer baserat på diagram och tolkning av sjökorten. Så för detta ändamål har jag skapat några enkla exceldiagram och tolkningar i samma kalkylblad.

Steg 1: Starta subrutinen i VBA. Nu för att komma åt PowerPoint har vi redan aktiverat PowerPoint-objektmodellen i de tidigare stegen, nu. För att komma åt detta måste vi deklarera variabeln som PowerPoint.Application.

Koda:

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

Steg 2: För att lägga till presentationen i PowerPoint måste vi deklarera en variabel som PowerPoint. Presentation.

Koda:

 Dim PPPresentation Som PowerPoint.Presentation

Steg 3: Efter att ha lagt till presentationen i PowerPoint måste vi lägga till Slide. Att deklarera variabeln som PowerPoint.Slide

Koda:

Dim PPSlide som PowerPoint.Slide

Steg 4: När bilden har lagts till i PowerPoint måste vi använda former i PowerPoint, dvs. textrutor. Att deklarera en variabel som PowerPoint.Shape

Koda:

Dim PPShape som PowerPoint.Shape

Steg 5: För att komma åt alla diagram i kalkylbladet måste vi deklarera variabeln som Excel.ChartObjects.

Koda:

Dim PPCharts som Excel.ChartObject

Okej, för att inleda förfarandet räcker det med dessa variabler.

Step 6: Now, we need to launch the PowerPoint from excel. Since it is an external object, we need to set this as a new PowerPoint.

Code:

Set PPApp = New PowerPoint.Application

This will launch the new PowerPoint from excel.

Step 7: Now, the variable PPApp is equal to the PowerPoint we have launched. Now make this PowerPoint visible and maximize the window.

Code:

PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized

At this moment, just run the code using the F5 key or manually. You should see the PowerPoint app launched like the below one.

Step 8: Now, we need to add a presentation to the PowerPoint app we have launched.

Code:

Set PPPresentation = PPApp.Presentations.Add

Now we should see the PowerPoint presentation like this.

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

Intressanta artiklar...