This Code adds the three Slides in PowerPoint. Refer to Image2. How it is adding and what are the Classes we are using in it. Let us take a deep dive into it.
‘Because Finding the right code is not important but understanding it line by line is very important. So, I am going to explain you. But before that let us understand how Powerpoint classes works in VBA. First of all, you need to go and select PowerPoint reference under Tools-> Reference (Refer to Image1). This reference will now allow us to have an access to each and every PowerPoint class. For eg: Presentations ,Slides.
You need to remember how Object hierarchy works in PowerPoint application. So,let me ask you how it works in Excel VBA. You have Excel object , then a Workbook, then a worksheet and in worksheet you have Cells and many others. So, if you don’t have Excel, you cannot go and access other objects. Isn’t it? Same principle applies on other applications. In PowerPoint, if we don’t have PowerPoint object, you cannot create a presentation on it and if you don’t have presentation , it is obvious there will be no Slide. So, every class , we will write line by line and in a proper way.
Sub MakeSlides() Dim PPT As PowerPoint.Application Set PPT = New PowerPoint.Application PPT.Visible = True Dim PPres As PowerPoint.Presentation ' This is a class and we have to use it for slides Set PPres = PPT.Presentations.Add ' adding the presentation .Check on f8 mode and see its magic Dim PSlide As PowerPoint.Slide ' define Slide now Set PSlide = PPres.Slides.Add(1, ppLayoutBlank) ' Adding first slide Set PSlide = PPres.Slides.Add(2, ppLayoutBlank) ' Adding second slide Set PSlide = PPres.Slides.Add(3, ppLayoutBlank) ' Adding third slide 'So if you want to add more,I know you must be thinking can I not have a short code because for 50 slides, do i need to write Above line 50 times ? Huh! Certainly not. We can create a For next Loop which I will explain in my next blog. Till then, you master on this. End Sub
Image2 (This is Final Output)