This is the code from the PowerPoint Basics section of the seminar ( mod02PowerPointBasics)
Option Explicit ' NOTE: These examples refer to slides 2 & 3, titled "PowerPoint Basics" Sub PowerPointBasics_1() ' The PowerPoint Ojbect Model (OM) ' Navigating the OM ' Everything in PowerPoint is an Object of one type or another ' To control PowerPoint, you'll work with its objects ' Some objects are collections of other objects ' Objects have Properties - things that describe the object ' Objects have Methods - things the object can do or that you can do to it ' The Object Model is the whole collection of PowerPoint objects ' arranged in hierarchical fashion. ' An upside downtree diagram, in other words. ' Search Help on "object model" ' Press F2 to see the Object Browser ' The top level (or trunk) of the tree is the Application ' That's PowerPoint itself. ' The Application object has properties: Debug.Print Application.Name ' Using Debug.Print instead of MsgBox saves a little time ' and we don't have to keep clicking the OK button ' Debug.Print "prints" its output in the Immediate window of ' the VB Editor (IDE) ' If it's not visible, use View, Immediate Window or press Ctrl+G ' The .Presentations property returns a collection of the current ' open presentations and by "drilling down" through the object model ' (swinging like Tarzan from branch to branch?) ' you can get to lower level properties. ' We use "dot notation" for this: Debug.Print Application.Presentations.Count ' We can refer to specific members of collections by number: Debug.Print Application.Presentations(1).Name ' So PowerPoint (the application) contains Presentations ' Presentations contain Slides ' Slides contain Shapes like rectangles and circles ' So it stands to reason that we can: Debug.Print Application.ActivePresentation.Slides(2).Shapes.Count ' But all this typing is starting to get tedious ' It's easier (for us) and faster (for PowerPoint) to do this: ' Use With to "drill down" object you want to work with ... With ActivePresentation.Slides(2).Shapes(2) ' then all you need to do is add the dot and work with each property Debug.Print .Name Debug.Print .Height Debug.Print .Width ' and finally, End With to tell VBA/PowerPoint that you're done End With ' and to save more time, we can nest these things With ActivePresentation.Slides(2).Shapes(2) Debug.Print .Name With .TextFrame.TextRange Debug.Print .Text Debug.Print .Font.Name End With End With End Sub Sub PowerPointBasics_2() ' Working with the currently selected shape ' Show the name: With ActiveWindow.Selection.ShapeRange(1) Debug.Print .Name End With ' Change the name and move it: With ActiveWindow.Selection.ShapeRange(1) ' Naming shapes can be VERY useful, as you'll see .Name = "My favorite shape" .Left = .Left + 72 ' points, or 1 inch End With End Sub Sub PowerPointBasics_3() ' Working with a named shape ' If you know the name of a shape (and you just saw how to name it) ' You can manipulate it directly: ' No need to go to the slide, no need to select the shape, Just Do It With ActivePresentation.Slides(2).Shapes("My favorite shape") .Top = .Top - 72 End With ' Slides can have names too With ActivePresentation.Slides(2) .Name = "My favorite slide" End With ' The slide's name stays with it ' even if we move it to a different position in the presentation ' And now we can do: With ActivePresentation.Slides("My favorite slide").Shapes("My favorite shape") .Height = .Height * 2 End With End Sub Sub PowerPointBasics_4() ' References to objects ' Variables can hold references to objects ' This is a bit tough to get your head around, so don't worry ' It'll become clearer as you see working examples ' For now, watch this: ' Dim a variable as a specific object type Dim oShape As Shape ' Set it to "point" to a specific shape: Set oShape = ActivePresentation.Slides("My favorite slide").Shapes("My favorite shape") ' Notice that we used our slide's name? ' From now on, we can treat oShape as though it really WERE the shape Debug.Print oShape.TextFrame.TextRange.Text oShape.TextFrame.TextRange.Font.Color.RGB = RGB(255, 0, 0) ' and so on ' Until we release the memory used by oShape Set oShape = Nothing End Sub Sub PowerPointBasics_5() ' Looping through all of the slides ' Looping through all of the shapes ' So far, we haven't done anything you couldn't do ' with your mouse, and do it more easily at that. ' One more little lesson, then the real fun starts. Dim x As Long ' we'll use X as a counter ' OK, I said always to give variables meaningful names ' But for little "throwaway" jobs like this, programmers often ' use x, y, and the like ' Let's do something with every slide in the presentation For x = 1 To ActivePresentation.Slides.Count Debug.Print ActivePresentation.Slides(x).Name Next x ' Or with every shape on one of the slides ' Since x is a "junk" variable, we'll just re-use it here ' And we'll use the With syntax to save some typing With ActivePresentation.Slides(3) For x = 1 To .Shapes.Count Debug.Print .Shapes(x).Name Next x End With ' ActivePresentation.Slides(3) End Sub Sub PowerPointBasics_6() ' Error Handling ' You can trust computer users to do one thing and one thing only: ' The Unexpected ' You can trust computers to do pretty much the same ' That's where error handling comes in ' What do you think will happen when I run this code? With ActivePresentation.Slides(42) MsgBox ("Steve, you moron, there IS no slide 42!") End With End Sub Sub PowerPointBasics_6a() ' Error Handling Continued ' Let's protect our code against boneheaded Steves ' If he does something that provokes an error, deal with it gracefully On Error GoTo ErrorHandler With ActivePresentation.Slides(42) MsgBox ("Steve, you moron, there IS no slide 42!") End With ' Words with a : at the end are "labels" ' and can be the destination of a "GoTo" command ' Using GoTo is considered Very Bad Form except in error handlers ' If we got here without error we need to quit before we hit the error ' handling code so ... NormalExit: Exit Sub ErrorHandler: MsgBox ("Error: " & Err.Number & vbCrLf & Err.Description) ' resume next ' exit sub Resume NormalExit End Sub
Click Next to continue