4.4. Creating a Procedure for Powerpoint

The procedure you'll create for PowerPoint is short and straightforward, but it can save the user enough effort over the long run to make it worthwhile. It adds a title slide to the active presentation, inserting a canned title that includes the current date and the company's name as the presenter.

Follow these steps to create the procedure:

  1. Start PowerPoint. If PowerPoint is already running, close it and restart it. If PowerPoint creates a default presentation on startup, close the presentation.

  2. Create a new presentation based on a template of your choosing. Make sure the default slide on the presentation has the Title Slide layout by right-clicking the slide, then choosing Layout Title Slide to apply it to the default slide.

  3. Press Alt+F11 to open the Visual Basic Editor.

  4. Make sure the Visual Basic Editor is set up as described in the section, "Setting Up the Visual Basic Editor for Creating the Procedures," earlier in this chapter.

  5. In the Project Explorer window, right-click anywhere in the VBAProject(Presentation1) item and choose Insert Module from the context menu. The Visual Basic Editor inserts a new module in the project, displays a Code window containing the code sheet for the module, and expands the project tree.

  6. Verify that the Visual Basic Editor has entered the Option Explicit statement in the declarations area at the top of the code sheet. If not, type the statement now.

  7. Press the F4 key to activate the Properties window for the new module. Alternatively, click in the Properties window.

  8. Type the name for the new module in the Properties window: General_Procedures.

  9. Press the F7 key or click in the Code window to activate it.

  10. Below the Option Explicit statement, type the Sub statement for the procedure and press the Enter key:

    Sub Add_Title_Slide

  11. The Visual Basic Editor enters the parentheses at the end of the Sub statement, a blank line, and the End Sub statement for you, and places the insertion point on the blank line:

    Sub Add_Title_Slide()
    
    End Sub

  12. Press the Tab key to indent the first line below the Sub statement.

  13. Now identify the objects you need by using the Help system. You'll be working with the active presentation, which is represented by the ActivePresentation object. Press F1 or choose Help Microsoft Visual Basic Help to display the Visual Basic Help task pane. Type activepresentation in the Search box and press the Enter key or click the Start Searching button. A list of links appears. Click the one that looks most relevant (often the first link in the list). You should then see the details about the ActivePresentation object, as shown in Figure 4.7.

    Figure 4.7. The ActivePresentation Property screen
  14. Click the link to the Presentation object on the ActivePresentation Property screen to display its Help screen.

  15. Click the Presentation Object Members (scroll to find it), and then scroll to locate the Slides object in the list. Click the Slides link (see Figure 4.8), then click a Slides link again (in the new help window) to display the information about the Slides Collection Object shown in Figure 4.9.

  16. From this screen, take two pieces of information: first, that a slide is represented by a Slide object (organized into the Slides collection), and second, that you use the Add method to create a new slide.

  17. Type a declaration for an object variable of the Slide object type to represent the slide the procedure creates. Notice that after you type as and a space, the Visual Basic Editor displays the list of available keywords. Type down through the list (type s and l) until you have selected Slide, and then press the Enter key to complete the term and start a new line of code:

    Dim sldTitleSlide As Slide

    Figure 4.8. Select the Slides object from the list.

    Figure 4.9. The Slides Collection Object help screen
  18. Use a Set statement to assign to the sldTitleSlide object a new slide you create by using the Add method. Type set sld and then press Ctrl+spacebar to make the Complete Word feature enter sldTitleSlide for you. Then type = activepresentation.slides.add(, using the Visual Basic Editor's assistance, so that the line reads as shown here:

    Set sldTitleSlide = ActivePresentation.Slides.Add(

  19. When you type the parenthesis, the Auto Quick Info feature displays for you the syntax for the Add method, as shown in Figure 4.10.

    Figure 4.10. The Auto Quick Info feature displays the syntax for the Add method when you type the parenthesis after the Add method.
  20. Type the Index argument, a colon, an equal sign, the value 1 (because the title slide is to be the first slide in the presentation), and a comma:

    Set sldTitleSlide = ActivePresentation.Slides.Add(Index:=1,

    Choosing Between Labeled and Implied Argument Lists

    When a method uses arguments, as the Add method does here, you can choose between specifying the argument names or omitting them and letting VBA infer the arguments from the order of the values or constants. For example, in this case, you can specify either Add(Index:=1, Layout:=ppLayoutTitle) or Add(1, ppLayoutTitle). The latter is more concise and easier to type in, but the former is much clearer to read.


  21. Break the statement to the next line with a line-continuation character (an underscore preceded by a space). Then type a tab to indent the new line, type the Layout argument, a colon, and an equal sign, and pick the ppLayoutTitle constant from the List Properties/Methods dropdown list, as shown in Figure 4.11.

    Figure 4.11. Choose the ppLayoutTitle constant for the Layout argument.
  22. Type the parenthesis to end the statement:

    Set sldTitleSlide = ActivePresentation.Slides.Add(Index:=1, _
                            Layout:=ppLayoutTitle)

  23. Press the Enter key to start a new line, and then press either the Backspace key or Shift+Tab to unindent the new line by one tab stop.

  24. You'll be working with the sldTitleSlide from here on, so create a With statement using it, and place the insertion point on the line between the With statement and the End With statement:

    With sldTitleSlide
    
    End With

  25. Next, the macro will manipulate the two items on the slide. To make it do so, you need to know the objects that represent them. You could use the Macro Recorder to find out, but this time, try a more direct method: place the insertion point on the line within the With statement and type . (a period) to display the List Properties/Methods dropdown list of available properties and methods for the Slide object.

  26. Sometimes the List Properties/Methods dropdown list is of little help, because you'll find so many possibly relevant properties and methods that you can't identify the property you need. But if you scan the list in this case, you'll see that the Shapes property (which returns the Shapes collection) is the only promising item.

  27. Press Ctrl+G, choose View Immediate, or click the Immediate Window button on the Debug toolbar to display the Immediate window for a bit of testing.

  28. Type the following exploratory statement into the Immediate window and press the Enter key to execute this statement:

    ActivePresentation.Slides(1).Shapes(1).Select

    (The Immediate window is a quick way to test individual lines of code without having to run the entire macro.) Now switch to PowerPoint's window to see if the item was, in fact, selected (whether it has a frame drawn around it). Press Alt+F11 or click the View Microsoft PowerPoint button on the Standard toolbar to display the PowerPoint window to verify that VBA has selected the first Shape object on the slide.

  29. OK, this is the right object to start with, but now you need to find out how to add text to the shape. Go back to the Code window (click in the Code window or press the F7 key). Press the Backspace key to delete the period, and then type it again to redisplay the list. Type te to jump down to the items in the list whose names start with text. Select the TextFrame item in the list, and then type a period to enter the term and display the next list. Scroll down the list, select the TextRange object, and type a period to enter the term and display the next list. In the next list, select the Text property. Type an equal sign to enter the term. Then type double quotation marks followed by the text to assign to the text property: Pollution Update: (with a space after it), double quotation marks, an ampersand, and the date (supplied by the Date function):

    Shapes(1).TextFrame.TextRange.Text = "Pollution Update: " & Date

  30. Assign information to the second Shape in the same way:

    Shapes(2).TextFrame.TextRange.Text = "JMP Industrials."

  31. The whole procedure should look like this:

    Sub Add_Title_Slide()
        Dim sldTitleSlide As Slide
        Set sldTitleSlide = ActivePresentation.Slides.Add(Index:=1, _
            Layout:=ppLayoutTitle)
        With sldTitleSlide
             .Shapes(1).TextFrame.TextRange.Text = _
                 "Pollution Update: " & Date
             .Shapes(2).TextFrame.TextRange.Text = _
                 "JMP Industrials"
        End With
    End Sub

  32. Press F5 to test the procedure. Notice the slides; then delete all slides from the presentation (press Shift while clicking a range of slides in the left pane, then press Delete).

  33. Press Alt+F, I, then click the Customize link and add a Quick Access Toolbar button for the Add_Title_Slide procedure.

  34. Save the presentation under a name such as Procedures.pptm.

  35. Create a new presentation; then test the toolbar button or menu item for the procedure. Close the presentation without saving changes.

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

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