Insert Image in a PowerPoint Presentation Using VBA
In this article I will explain how you can insert an image into a PowerPoint presentation using VBA.
Sub main() Dim objPresentaion As Presentation Dim objSlide As Slide Dim objImageBox As Shape Set objPresentaion = ActivePresentation Set objSlide = objPresentaion.Slides.Item(2) Set objImageBox = objSlide.Shapes.AddPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", msoCTrue, msoCTrue, 100, 100) End Sub
The code above inserts the image located in the path “C:\Users\Public\Pictures\Sample Pictures\Desert.jpg” onto slide number “2”. For more information about the parameters to the function please see the link below:
Contents
Case 1: Inserting image into a slide without an image holder:
Consider the slide below:
As you can see the slide has 2 shapes which are both textboxes. The code above will yield the following result:
The 4th and 5th parameters are the distance from the left and top sides of the screen respectively.
Note: By supplying the optional 6th and 7th parameters you can set the width and height of the inserted image respectively.
Case 2: Inserting image into a slide with a single image holder:
Consider the slide below that has 1 image holder:
By calling the function, the image will be inserted inside the image holder and it will be resized to fit the image holder.
Note: The 4th, 5th, 6th and 7th parameters to the AddPicture
function will have no effects in this case. You can modify the position and size of the image by using the code below:
objSlide.Shapes.Item(2).Top = 300 objSlide.Shapes.Item(2).Left = 500 objSlide.Shapes.Item(2).Width = 300 objSlide.Shapes.Item(2).Height = 100
Case 3: Inserting image into a slide with more than one image holder:
In this case every time you call the function, the image will be inserted in one of the image holders:
2 thoughts on “Insert Image in a PowerPoint Presentation Using VBA”