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.

8 comments
alexsamson
alexsamson

How do I modify this script so that the image I insert is added to multiple slides - for example, a corporate logo?

R.Nitin
R.Nitin

I need VBA code to insert different pictures in different powerpoint slides and to resize them

RAVIARORARAVI
RAVIARORARAVI

Can someone send me the macro reqd to be inserted in an excel sheet which does following: The macro should update an excel sheet, ie the excel sheet in which now() function is being used in one column, after every five minutes without touching the key board so that the latest value as given out by now is used.

dianegroth
dianegroth

...and this is suppose to work in PowerPoint 2007?? I get this "Compile error: Syntax error"

bsteingraber
bsteingraber

Ok, so that inserts the same picture, but what if I wanted to insert a bunch of different pictures one per slide?

mbells
mbells

if you are going to have a graphic on many slides, put it on the master. 1/ it will be in the right place 2/ it takes up less space, since only 1 copy of the graphic is loaded not multiple copies

ssharkins
ssharkins

Master slide is a great solution if you're using the picture on most of your slides.

ssharkins
ssharkins

1.) I'd only automate the process if I inserted a lot of grahics on multiple slides, but not all slides. 2.) Simply pass the graphic file name when executing the VBA procedure. How you get the list to begin with would be the bigger problem.

Editor's Picks