Microsoft

Use VBA to insert a picture file onto a PowerPoint slide

If you often insert the same graphic file, such as a company logo onto a PowerPoint slide, use VBA to automate the task.
Inserting graphics is a big part of creating most PowerPoint presentations. A lot of graphics are unique to the slide and you use them just once. But other graphics, such as a logo, you might use often. Inserting the same file isn't a big deal because the process is easy, but there's no reason to work harder than you must. If you have a graphic file you insert often, automate the process using a VBA procedure (macro), as follows:

  1. In PowerPoint, launch the Visual Basic Editor by pressing [Alt]+[F11].
  2. Choose Module from the Insert menu.
  3. Enter the following subprocedure:

Sub InsertLogo()
  'Insert TR logo.
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:="C:\Active\TR\September2009Blog\TR.jpg", _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=35, _
   Width:=98, Height:=48).Select
End Sub

Be sure to update the filename and path for the FileName argument. The Left and Top values enter the logo file in the top-left corner of the current slide. You might consider using the macro recorder to provide the location and dimension values, but most likely, you'll want to tweak the results.

To run the macro, select a slide and choose Macro from the Tools menu. Then, choose Macros, select InsertLogo, and click Run. PowerPoint will insert the graphic file in the top-left corner of the current slide.

If the file isn't in the specified folder, the macro will generate an error, so you'll want to accommodate that possibility. In addition, if you have several files you insert frequently, you might want to use a function instead of a subprocedure. That way, you can pass filenames as follows:

Function InsertLogo(gfilename As String)
  'Insert TR logo.
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:=gfilename, _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=35, _
   Width:=98, Height:=48).Select
End Function

The passed string, gfilename, must specify the entire pathname if the file isn't in the same folder as the presentation file.

About

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks