PPT2001: Introduction to Macro Programming in PowerPoint 2001 (274749)



The information in this article applies to:

  • Microsoft PowerPoint 2001 for Macintosh

This article was previously published under Q274749

SUMMARY

This article walks you through the steps to create a simple Microsoft Visual Basic for Applications macro within Microsoft PowerPoint. The macro you create will add a slide to your presentation, set a background texture for a slide, set slide timings, and run a slide show.

This article is designed to introduce you to some of the tools and concepts you need to become a macro programmer.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Create a New Presentation

  1. On the File menu, click Project Gallery.

    This opens up the Project Gallery dialog box.
  2. Select the PowerPoint Presentation icon under the Blank Documents category, and click OK.

    This opens up the New Slide dialog box.
  3. Select the Blank AutoLayout in the lower-right corner, and click OK.
You now have a blank presentation open, ready to create the macro.

Create a Macro

  1. On the Tools menu, point to Macro, and then click Macros.

    This opens up the Macro dialog box.
  2. In the Macro name box, type YourMacro as the name for your macro.

    NOTE: Macro names must begin with a letter and can contain up to 80 characters. Visual Basic for Applications keywords are invalid names for macros. The name cannot contain any spaces. Programmers typically use an underscore character(_) to separate words.

    If you type an invalid macro name, you receive the following message
    macro name is not a valid name for a macro
    where macro name is the name that you typed for the macro.
  3. Click Create.

    This opens the Visual Basic Editor. The Visual Basic Editor is an area where you can create, edit, and debug your macros.

    In a Code window, you see the following:
    Sub YourMacro ()
       '
       ' Macro created <date> by <YourName>
       '
    
    End Sub
    					

Add Code to a New Macro

You are now looking at a flashing cursor within the Code window. The Code window is where you actually type Visual Basic commands. For the most part, the Code window acts like a typical text editor, enabling you to cut, copy, and paste text. However, there are some differences that make it easier for you to create macros. The important differences are detailed below.
  1. Type the following line of code right above the line that contains the words "End Sub":
    Dim oSlide As Slide
    						
    What does this code do?
       Dim        Indicates to the Visual Basic Editor you are about to declare
                  a variable. There are several other methods available to
                  declare variables, but this article discusses only the Dim
                  method.
    
       oSlide     Is the name you provide the variable. It is a good idea to
                  give your variables meaningful descriptive names. X is an
                  example of a poor variable name. Meaningful names make your
                  code easier to read.
    
       As Slide   Specifies the type of data the variable will contain. In
                  this case, oSlide will have the data type Slide.
    					
  2. Let's add some more code. Type the following line of code after the variable declaration:
    Set oSlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)
    						
    This code adds a new slide to the active presentation. The slide created uses the Title Only AutoLayout. Lets take a closer look at this line of code.
       Set oSlide               Assigns an object reference to a variable or
                                property. Using Set makes it easier to refer
                                to that same object later in your code.
    
       ActivePresentation       Tells the Visual Basic Editor you are referring
                                to the presentation that is open in the active
                                window.
    
       Add(1, ppLayoutTitle)    Creates a new slide and adds it to the
                                collection of slides in the presentation.
                                The Add method takes two parameters:
    
                                The first parameter, the slide index, is the
                                number 1 in this case. The slide index is the
                                position where PowerPoint creates the slide.
                                When the index is set to 1, PowerPoint creates
                                the new slide at the beginning of the
                                presentation.
    
                                The second parameter specifies the type of
                                AutoLayout.
    						
    For more information about creating slides programmatically, from the Visual Basic Editor, click the Office Assistant, type how to add a slide, click Search, and then click to view "Add Method (Slides Collection Object)."

    NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, click the article number below to view the article in the Microsoft Knowledge Base:

    290590 OFF2001: Office Assistant Not Answering Visual Basic Questions

    Your macro code now looks something like this:
    Sub YourMacro ()
       '
       ' Macro created <date> by <YourName>
       '
       Dim oSlide As Slide
       Set oSlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)
    
    End Sub
    						
    NOTE: The text following ' apostrophe (on the same line) is a comment. Comments are ignored by the Visual Basic Editor. They are added to the code to make it easier to understand what is going on in the code.

    Now that your macro actually does something, you can try running the macro.

Running the Macro

There are several methods to run a macro. Only one method is described in this article.
  1. On the File menu, click Close and Return to Microsoft PowerPoint.

    The Visual Basic Editor closes and you return to PowerPoint.
  2. On the Tools menu, point to Macro, and then click Macros.

    This opens up the Macro dialog box.
  3. Select your macro from the list and then click Run.
PowerPoint adds a new slide to the beginning of your presentation. Now let's add some more code.

Editing the Macro Code

To view the source code of a specific macro, follow these steps:
  1. On the Tools menu, point to Macro, and then click Macros.

    This opens up the Macro dialog box.
  2. Click the macro that you want to edit.
  3. Click Edit.

    This opens the macro within the Visual Basic Editor. The Visual Basic Editor is where you make corrections, remove unnecessary steps, or add instructions that you can't record in PowerPoint.

Add Some More Code

You are now ready to add the rest of the commands to complete the macro.
  1. Type the following as the next line of code in your macro:
    ActiveWindow.ViewType = ppViewSlideSorter
    						
    This changes the presentation to the slide sorter view. We are doing this so we can select the entire slide, including the slide itself.
  2. Add the next line of code to your macro:
    oSlide.Select
    						
    This method selects the slide that you created. oSlide is the object reference you created with the Dim statement.
  3. Add the next section of code to your macro:
    With ActiveWindow.Selection.SlideRange
       .FollowMasterBackground = msoFalse
       .Background.Fill.PresetTextured msoTextureRecycledPaper
    End With
    						
    These commands tell PowerPoint that this particular slide does not follow the master, and then set the background preset texture to the recycled paper.

    The With statement allows you to group commands that have common references. Using With to group multiple commands can improve the performance of the macro as well as saving you a lot of typing.

    If you didn't use the With statement, your code would look like this:
    ActiveWindow.Selection.SlideRange.FollowMasterBackground = msoFalse
    ActiveWindow.Selection.SlideRange.Background.Fill.PresetTextured _
       msoTextureRecycledPaper
    						
    The underscore (_) in the second line is a continuation character. It tells the Visual Basic Editor that you could not fit the specific command on one line and are continuing the instruction on the next line.

    You can see the advantage of using With statements: less typing and faster code. The main disadvantage of the With statement is that it sometimes makes the code more difficult to read, especially if you nest a With within another With statement.
  4. Add the next line of code to your macro:
    oSlide.Shapes.Title.TextFrame.TextRange.Text = "Look What I Did!"
    						
    This command adds the text "Look What I Did!" into the title box of the slide that you created.
  5. Add the next section of code to your macro:
    With ActivePresentation.Slides.Range.SlideShowTransition
       .AdvanceTime = 5
       .EntryEffect = ppEffectCheckerboardAcross
    End With
    					
       AdvanceTime    Specifies how long (in seconds) a particular
                      slide is visible when running a slide show.
    
       EntryEffect    Specifies the slide transition effect that runs just
                      prior to the slide appearing.
    					
  6. Add the last line of code to your macro:
    ActivePresentation.SlideShowSettings.Run
    						
    This line of code starts the presentation as a slide show.

The Complete Macro Code

Sub YourMacro()
   '
   ' Macro created <Date> by <You>
   '
   Dim oSlide As Slide

   ' Add a new slide to the presentation.
   Set oSlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)

   ' Change the presentation to slide sorter view.
   ActiveWindow.ViewType = ppViewSlideSorter

   ' Select your slide.
   oSlide.Select

   ' Apply a preset texture to the slide.
   With ActiveWindow.Selection.SlideRange
      .FollowMasterBackground = msoFalse
      .Background.Fill.PresetTextured msoTextureRecycledPaper
   End With

   ' Add text into title of the slide.
   oSlide.Shapes.Title.TextFrame.TextRange.Text = "Look What I Did!"

   ' Set the slide timing and transition effect.
   With ActivePresentation.Slides.Range.SlideShowTransition
      .AdvanceTime = 5
      .EntryEffect = ppEffectCheckerboardAcross
   End With

   ' Start the slide show.
   ActivePresentation.SlideShowSettings.Run

End Sub
				

REFERENCES

For more information about creating Visual Basic for Applications macros, click the Office Assistant, type create a macro in Visual Basic Editor, click Search, and then click to view the topic.

For more information about running Visual Basic for Applications macros, click the Office Assistant, type run a macro, click Search, and then click a topic to view it.

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbcode kbdtacode kbhowto kbmacro kbProgramming KB274749