3.4. Editing a Powerpoint Macro

In this section, you'll edit a PowerPoint macro. PowerPoint no longer includes a macro recorder, so you'll either have to type in the code for the following example, or, better, just copy it from this book's Web site at: www.sybex.com/masteringvba.

Start by opening the PowerPoint Visual Basic Editor:

  1. Open PowerPoint and in the new, blank presentation that's displayed, add a shape by clicking the Insert tab on the Ribbon, then clicking the Shapes icon in the Illustrations section.

  2. Click a rectangle shape of your choice. This will be object 1 in the Shapes collection, so we can refer to it in the code like this:

    ActiveWindow.Selection.SlideRange.Shapes(1).Select

  3. Open the PowerPoint Visual Basic Editor by pressing Alt+F11.

  4. Copy the code shown in Listing 3.6.

    Example 3.6. Listing 3.6
    1.  Sub Add_Slide_and_Format_Placeholder()
    2.  '
    3.  ' Sample macro that adds a slide, formats its placeholder, and adds
      text _
            to it. Recorded 12/4/08 by Rodney Converse.
    4.  '
    5.      ActiveWindow.View.GotoSlide Index:= _
                ActivePresentation.Slides.Add(Index:=2, _
                Layout:=ppLayoutText).SlideIndex
    6.      ActiveWindow.Selection.SlideRange.Layout = ppLayoutTitle
    7.      ActiveWindow.Selection.SlideRange.Shapes(1).Select
    8.      With ActiveWindow.Selection.ShapeRange
    9.           .IncrementLeft −6#
    10.          .IncrementTop −125.75
    11.     End With
    12.     ActiveWindow.Selection.ShapeRange.ScaleHeight 1.56, msoFalse, _
                msoScaleFromTopLeft
    13.     ActiveWindow.Selection.SlideRange.Shapes(1).Select
    14.     ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
    15.     ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters _
                (Start:=1, Length:=0).Select
    16.     With ActiveWindow.Selection.TextRange
    17.          .Text = "The quick brown dog jumped over a lazy fox"
    18.          With .Font
    19.              .Name = "Arial"
    20.              .Size = 44
    21.              .Bold = msoFalse
    22.              .Italic = msoFalse
    23.              .Underline = msoFalse
    24.              .Shadow = msoFalse
    25.              .Emboss = msoFalse
    26.              .BaselineOffset = 0
    27.              .AutoRotateNumbers = msoFalse
    28.              .Color.SchemeColor = ppTitle
    29.          End With
    30.      End With
    31.      ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters _
                 (Start:=1, Length:=42).Select
    32.      With ActiveWindow.Selection.TextRange.Font
    33.          .Name = "Impact"
    34.          .Size = 54
    35.          .Bold = msoFalse

    36.          .Italic = msoFalse
    37.          .Underline = msoFalse
    38.          .Shadow = msoFalse
    39.          .Emboss = msoFalse
    40.          .BaselineOffset = 0
    41.          .AutoRotateNumbers = msoFalse
    42.          .Color.SchemeColor = ppTitle
    43.      End With
    44.  End Sub

Here's what happens in the macro:

  • Line 1 starts the macro, and line 44 ends it.

  • Lines 2 and 4 are blank comment lines used to set off the description of the macro, which appears in line 3.

  • Line 5 adds the slide to the presentation. This statement is a little complicated, but don't worry about it too much just yet. For now, note two things: First, the statement uses the Add method with the Slides collection object to add a slide to the collection (in other words, to create a new slide), just as the Excel macro used the Add method to add a workbook to the Workbooks collection. Second, the layout of the slide is ppLayoutText, the VBA constant for the Text slide layout that PowerPoint uses for a default new slide.

  • Line 6 applies the Title layout (ppLayoutTitle) that you chose when recording the macro. (If you chose a different slide layout, you'll see a different constant than ppLayoutTitle.)

  • Line 7 selects the first shape in the Shapes collection on the active slide. (For the moment, don't worry about how you get to the active slide.)

  • Lines 8 to 11 contain a With statement that works with the shape that has been selected (ActiveWindow.Selection.ShapeRange). A With statement is a way of simplifying object references, and everything between the With statement and the End With statement refers to the objects that the With statement mentions. In this case, line 9 uses the IncrementLeft method with a negative value to move the shape to the left, and line 10 uses the IncrementTop method with a negative value to move the shape up the slide.

    The With Command Has Two Uses

    With statements have two benefits: they simplify code (because you don't need to specify the object in each of the lines between the With and End With lines), and they make code run faster.


  • Line 13 selects the first shape in the Shapes collection, and line 14 selects the TextRange object in the TextFrame object in the shape. When you're working interactively, PowerPoint makes this selection process seamless: you click in a shape displaying the legend "Click to add title" (or whatever), and PowerPoint selects the text range in the text frame in the shape — but all you see is that the text in the shape becomes selected. In VBA, you have to go through a couple of unseen layers in the object model before getting to the text.

  • When you select the placeholder text, PowerPoint gets rid of it. The same thing happens when you select the placeholder text via VBA. So line 15 makes a new selection at the beginning of the first character in the text range. The Length of the selection is 0, meaning that the selection is collapsed to an insertion point rather than containing any characters.

  • Line 16 starts a With statement that continues until line 30. The With ActiveWindow.Selection.TextRange statement in line 16 lets line 17 reference the Text property of the TextRange object in the Selection object in the ActiveWindow object much more simply (instead of ActiveWindow.Selection.TextRange.Text), and it lets line 18 reference the Font property of the TextRange object in the Selection object in the ActiveWindow object easily (instead of ActiveWindow.Selection.TextRange.Font).

  • Line 17 sets the Text property of the ActiveWindow.Selection.TextRange object to the text typed.

  • Line 18 then begins a nested With statement that sets the properties of the Font object for the TextRange object. Line 19 sets the Name property of the Font object to Arial; line 20 sets the Size property of the Font object to 44; line 21 sets the Bold property of the Font object to msoFalse, the Microsoft Office (mso) constant for False; and so on. The Macro Recorder created all these separate statements to record the font formatting used when the text was typed, and it's not necessary to the macro. Line 29 ends the nested With statement.

    With Blocks Can Be Nested

    A nested With statement is one that is placed within another With statement, and specifies an object within the object specified in the outer With statement. You can nest multiple-level With statements when necessary.


  • Line 31 uses the Select method to select characters 1 through 42 in the text range. This is the result of pressing the Ctrl+Shift+Home key combination. Because this statement specifies the characters to select, you'll need to change it if you change the text that the macro inserts. (If you run the statement on a text range that has fewer than 42 characters, it will return an error. If you run it on a text range that has more than 42 characters, it will select only the first 42 characters in the text range — not what you want.)

  • Line 32 begins another With statement that works with the Font object of the TextRange object — but this With statement records the actions you took in the Font dialog box, so you want to keep it.

  • Line 43 ends the With statement, and line 44 ends the macro.

Edit this macro by slimming it down a little and changing the text it inserts:

  1. Delete the unnecessary With statement in lines 18 through 29.

  2. Delete line 30.

  3. Change lines 16 and 17 into a single statement without With:

    ActiveWindow.Selection.TextRange.Text = _
        "The quick brown dog jumped over a lazy fox"

  4. Now change the text that the new line 16 inserts. Type text of your choice between the double quotation marks.

  5. Change line 31 to use the Select method on the text range rather than specifying which characters to select. Delete Characters(Start:= 1, Length:= 42) to leave this statement:

    ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select

  6. Click the Save button on the Standard toolbar or choose File Save to save the changes you've made to the presentation. In the Save As dialog box, locate the Save As Type dropdown list and change it from the default .pptx type (which cannot contain macros) to the .pptm type (which can).

  7. You should now have code that reads like Listing 3.7.

    Example 3.7. Listing 3.7
    1.   Sub Add_Slide_and_Format_Placeholder()
    2.  '
    3.  ' Sample macro that adds a slide, formats its placeholder, and adds text
      to it. _
          Recorded 12/4/08 by Rodney Converse.
    4.  '
    5.      ActiveWindow.View.GotoSlide Index:= _
                ActivePresentation.Slides.Add(Index:=2, _
                Layout:=ppLayoutText).SlideIndex
    6.      ActiveWindow.Selection.SlideRange.Layout = ppLayoutTitle
    7.      ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
    8.      With ActiveWindow.Selection.ShapeRange
    9.           .IncrementLeft −6#
    10.          .IncrementTop −125.75
    11.     End With
    12.     ActiveWindow.Selection.ShapeRange.ScaleHeight 1.56, msoFalse, _
                msoScaleFromTopLeft
    13.     ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
    14.     ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
    15.     ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters _
                (Start:=1, Length:=0).Select
    16.     ActiveWindow.Selection.TextRange.Text = "Welcome to Acme Industries"
    17.     ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
    18.     With ActiveWindow.Selection.TextRange.Font
    19.         .Name = "Impact"
    20.         .Size = 54
    21.         .Bold = msoFalse
    22.         .Italic = msoFalse
    23.         .Underline = msoFalse
    24.         .Shadow = msoFalse
    25.         .Emboss = msoFalse
    26.         .BaselineOffset = 0
    27.         .AutoRotateNumbers = msoFalse
    28.         .Color.SchemeColor = ppTitle
    29.     End With
    30. End Sub

Now step through the changed macro and make sure it works as you expect it to.

3.4.1. Save Your Work

When you finish working with this macro, choose File Save from the Visual Basic Editor to save the presentation that contains the macro and the changes you've made to it. If necessary, change the file type from the default .pptx to the macro-enabled .pptm type. Then press Alt+Q or choose File Close and Return to Microsoft PowerPoint to close the Visual Basic Editor and return to PowerPoint.

When Should You Use the Macro Recorder?

As you've seen so far in this book, you can create VBA code either by using the Macro Recorder (in the applications that provide it) to record a series of actions when working interactively in the application or by entering VBA statements into the Code window in the Visual Basic Editor. You're probably wondering when you should record a macro and when you should create code from scratch. Writing a procedure is more difficult and more advanced than recording a procedure — so is it wrong to record a procedure when you could write it instead?

Using the Macro Recorder has advantages and disadvantages. The advantages are:

  • The Macro Recorder creates usable code every time (provided you run the macro under suitable conditions).

  • The Macro Recorder is quick and easy to use.

  • The Macro Recorder can help you discover which VBA objects, methods, and properties correspond to which part of an application's interface.

The disadvantages of using the Macro Recorder are:

  • Code created in the Macro Recorder may contain unnecessary statements, because the Macro Recorder records everything you do in the application — including all the options in every built-in dialog box you use when recording the macro. For example, if you start the Macro Recorder from Word, choose Tools Options to display the View page of the Options dialog box, click the Edit tab to display the Edit page, and change the Auto-Keyboard Switching setting, the Macro Recorder will record all the settings on the Edit page as well as all those on the View page. The result is about 40 lines of unnecessary code. (If you visit any other pages in the Options dialog box on the way to the Edit page, the Macro Recorder will record all the settings in those pages as well.) If you create the code manually in the Visual Basic Editor, you can achieve the same effect by using one statement.

  • Code created by the Macro Recorder can work only in the active document rather than using other documents, because whichever document you're working with interactively becomes the active document. Later in this book, you'll learn how to use objects in the applications' object models to work with documents other than the active document. Working with other documents can have advantages; for example, you can hide the manipulations you're performing from the user and make your code run faster.

  • The Macro Recorder can create VBA code for only some of the actions you perform in the host application. For example, if you want to display a dialog box or a user form in the course of a procedure, you need to write the appropriate statement manually — you can't record it. The subset of VBA actions available through the Macro Recorder is similar to the set of actions you can take in the host application when working interactively, so you can get a lot done with it. Still, you'll find it's limited compared to the full range of actions you can perform through VBA.

However expert you become with VBA, consider the Macro Recorder a useful tool for creating either rough-and-ready macros or the basis of more complex procedures. You'll often find it makes sense to have the Macro Recorder handle as much of the strain of creating a procedure as possible. If you can save time by using the Macro Recorder to quickly identify the VBA object or property that you need, then do so. In addition, the Macro Recorder can show you how to write some code that you can't figure out how to write on your own. The Recorder always gets the syntax right.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.24.52