Previous  Home  Next

Import pictures at proper size

When you import a picture manually, PowerPoint makes a reasonable guess as to what size it should be.
No matter what size it decides upon, it doesn't distort the image on import.

When importing an image in VBA, PowerPoint insists that you tell it what size the image should be.
In most cases, we have no idea what to tell it. So how do we import an image without distorting it?
Like so:

Sub ImportPictureAtSize()

	Dim oSlide As Slide
	Dim oPicture As Shape

	' Set oSlide to the first slide in the presentation (modify as needed to suit your own purposes)
	Set oSlide = ActiveWindow.Presentation.Slides(1)

	' Set oPicture to the picture file on your computer. Set Link To
	' File to false, Save With Document to true, and place it in the
	' upper left-hand corner of the slide, sized arbitrarily.
	' NOTE: Before you run this code replace this text string:
	' "Put image path here!"
	' with the path to the image you want to import. For example:
	' "c:\MyImage.bmp"

	Set oPicture = oSlide.Shapes.AddPicture("C:\mcp.bmp", _
		msoFalse, msoTrue, 1, 2, 3, 4)

	' Now scale the picture to full size, with "Relative to original
	' picture size" set to true for both height and width.
	' This resets the picture to whatever size PowerPoint would normally 
	' have imported it at:	

	oPicture.ScaleHeight 1, msoTrue
	oPicture.ScaleWidth 1, msoTrue

	' And if you'd like to center the picture:
	With ActivePresentation.PageSetup
		oPicture.Left = (.SlideWidth \ 2) - (oPicture.Width \ 2)
		oPicture.Top = (.SlideHeight \ 2) - (oPicture.Height \ 2)
	End With

End Sub
Previous  Home  Next