PPT: Introduction to Macro Programming in PowerPoint 97 and 98

ID: Q162102

The information in this article applies to:

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Create A New Presentation

1. On the File menu, click New. This opens up the New Presentation dialog

   box.

2. Select the Blank Presentation icon on the General tab and click OK.
   This opens up the New Slide dialog box.

3. Select the Blank AutoLayout, located in the bottom 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 in a name for your macro. The name 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.

   Don't worry about trying to remember all the rules for naming macros.
   If you enter an invalid macro name, PowerPoint will display a dialog
   box with a message similar to the following:

      <macro name> is not a valid name for a macro

   If you see this message, just keep trying until you find a name that
   PowerPoint will accept.

3. Click the Create button. This opens the Visual Basic Editor. The Visual
   Basic Editor is an application where you can create, edit, and debug
   your macros.

Add Code to a New Macro

You should now be looking at a flashing cursor within the Code Window. The Code Window is where you actually type Visual Basic commands. A recorded macro can also be viewed in the code window. 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 between the Sub and End Sub:

      Dim MySlide As Slide

   When you were typing in the code, you probably noticed some interesting
   things happen. After you hit the spacebar following the word as, a drop
   down list of the available data types appeared on your screen. This is
   just one of the ways the Visual Basic Editor makes programming a little
   easier.

   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.

   MySlide    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, MySlide will have the data type Slide.

2. Let's add some more code. Type the following line of code after the
   variable declaration:

      Set MySlide = ActivePresentation.Slides.Add(1, ppLayoutTitle)

   This code adds a new slide to the active presentation. The slide
   created uses the Title Only auto layout. Lets take a closer look at
   this line of code.

   Set MySlide              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 on creating slides programmatically, search for
   "Add Slides" using the Help menu.

   For more information on the slide AutoLayouts available, search for
   "PpPlaceHolder Type" using the Help menu.

   TIP: When entering code, if the property and method list pops up, you
   can select the item you want and then press TAB, which adds
   the object to your command and leave the cursor on the same line.

   Your macro code should now look something like this:

      Sub YourMacro ()
         '
         ' Macro created 1/7/97 by You
         '
         Dim MySlide As Slide
         Set MySlide = 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, we can try running the
   macro.

Run the Macro

There are several methods to run a macro. Only one method is described in this article.

1. Click Close and Return to Microsoft PowerPoint from the File menu. The

   Visual Basic Editor closes and you return to PowerPoint.

2. On the Tools menu, point to Macro, and then click Macros. This opens the
   Macro dialog box.

3. Select your macro from the list and then click the Run button.

PowerPoint adds a new slide to the beginning of your presentation. Now let's add some more code.

View the Macro Code

Use the following steps to view the source code of a specific macro.

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

   up the Macro dialog box.

2. Click the macro you want to edit. The name of the macro should appear
   highlighted.

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

Add Some More Code

Now we are ready to add the rest of the commands to complete the macro.

1. Add the next line of code to your macro:

      ActiveWindow.ViewType = ppViewSlideSorter

   This changes the presentation to slide sorter view. We are doing this
   so we can select the entire slide.

   For more information on PowerPoint views, search for "views" using the
   Help menu.

2. Add the next line of code to your macro:

      MySlide.Select

   This code selects the slide you created. MySlide 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.

   Without using the With statement the 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 only downside to the With statement is 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:

      MySlide.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 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 code starts the slide show.

The Complete Macro Code

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

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

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

      ' Select your slide.
      MySlide.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.
      MySlide.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 in Microsoft PowerPoint, type "how to create a macro," click Search, and then click to view "Create a macro in Visual Basic Editor."

For more information about running Visual Basic for Applications macros, click the Office Assistant in Microsoft PowerPoint, type "how to run a macro," click Search, and then click to view "Run a macro."

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, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: 97 8.00 kbmacro kbpptvba ppt8 vba vbe 98 macppt
Keywords          : kbcode kbmacro kbprg kbdta kbdtacode kbpptvba 
Version           : WINDOWS:97; MACINTOSH:98
Platform          : MACINTOSH WINDOWS
Hardware          : MAC x86

Last Reviewed: May 17, 1999