1

Here is code I created for inserting and resizing the image (one per slide). I couldn't crop the images inserted from the file location.

The process should be:

  1. Insert the image from file location to the existing slide.
  2. Crop the image to the required dimension.
  3. Resize the image to the required size.

Note: I have around 40 images in one file and I need to insert them and crop each in one slide and each image have different dimension and size.

I created the below code for individual image.

Sub insert()

    i = 1

    Set myDocument = ActivePresentation.Slides(i)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

    Set myDocument = ActivePresentation.Slides(i + 1)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image2.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

    Set myDocument = ActivePresentation.Slides(i + 2)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image3.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

    Set myDocument = ActivePresentation.Slides(i + 3)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image4.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

    Set myDocument = ActivePresentation.Slides(i + 4)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image4.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

    Set myDocument = ActivePresentation.Slides(i + 5)
    myDocument.Shapes.AddPicture Filename:="D:\Pictures\" & _
                                            "image5.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                                 Left:=390, Top:=200, Width:=330, Height:=250

End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 1
    What have you tried so far (show your code)? Where do you run into troubles? – Shrotter Oct 21 '22 at 07:12
  • 1
    You shouldn't duplicate the code - just pass the values as parameters – FunThomas Oct 21 '22 at 07:35
  • Can someone please help me out – Tejesh Goud Oct 24 '22 at 10:56
  • @TejeshGoud - Please let me know if my answer helped you out, or if you require additional assistance. – Justin Edwards Oct 26 '22 at 00:10
  • @Justin Edwards - Hi Justin thanks for your efforts on this, actually in the code shown above have the same size for all the images but in the real case scenario i need to make changes to each image size manually as per requirement. And also I am satisfied with above code but after inserting in existing slide I need to crop the image because the image have excess white area which is not reqiured, so I need to crop the image after inserting and then I need to resize the image as per the requirement and placement. – Tejesh Goud Oct 27 '22 at 00:49
  • But the above code only insert the image and resizing it. I also need to crop it before resizing it. Thanks in advance – Tejesh Goud Oct 27 '22 at 00:50
  • @TejeshGoud - Understood. Per your request, I have developed a cropping function that identifies white borders and automatically crops them. Please remember to mark the solution as answer if it pleases you. – Justin Edwards Oct 27 '22 at 10:40
  • @Justin Edwards - Thanks for your interest and not only white area but i need to crop it to the required size. If you don't mind can you please add cropping code to the below code. that will help a lot to me. Sub insert() i = 1 Set myDocument = ActivePresentation.Slides(i) myDocument.Shapes.AddPicture FileName:="D:\Pictures\" & _ "image1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _ Left:=390, Top:=200, Width:=330, Height:=250 The code you provided is not working for me. please help me. Thanks – Tejesh Goud Oct 27 '22 at 12:53
  • @TejeshGoud - A basic cropping example has been added to the answer – Justin Edwards Oct 27 '22 at 14:38
  • @Justin Edwards - Thanks a lot Justin it's working fine. – Tejesh Goud Oct 27 '22 at 15:23
  • @TejeshGoud - Glad to hear it. I apologize if the complexity of my answer made it difficult to parse out the desired solution. Also, I realize in retrospect that setting specific layouts is not a great idea because it moves stuff around within each slide in undesirable ways, and I have subsequently removed them from the advanced example. I also discovered through experimentation that the picture layout can cause an issue with certain file sizes, so I added some additional code to handle that. Hopefully, this contribution helps the community, and please let me know if you need anything else. – Justin Edwards Oct 28 '22 at 04:41

1 Answers1

0

To merely crop a shape, here is the basic code:

Sub insert()
    i = 1
    Set myDocument = ActivePresentation.Slides(i)
    myDocument.Layout = ppLayoutPictureWithCaption
    Set myImage = myDocument.Shapes.AddPicture("D:\Pictures\" & _
    "image1.PNG", LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
        Left:=390, Top:=200, Width:=330, Height:=250)
    myImage.PictureFormat.CropLeft = 40
    myImage.PictureFormat.CropRight = 40
    myImage.PictureFormat.CropTop = 40
    myImage.PictureFormat.CropBottom = 40
End Sub

For a more advanced example, using a specific directory, it is possible to loop through the files and add all of the images therein to each consecutive slide. In the code below, I have added error handling that will create slides if there are none present or if there are less slides than there are pictures.

To automatically crop a solid color from around images, it's a little more complicated, but it is doable using the getPixel API. In the below example, I create a temporary file in bitmap format, and loop through the pixels from the left side of the image until a non-white pixel is found to determine the width of the border. I then crop the shape accordingly. I've tested this code in different environments, and I've discovered that there are differences in the size of the exported files. Consequently, there is a scaleAdjustment parameter that can be used to dial in the cropping for a specific system. In the code below, the crop color is set to white, but if a different color needs to be cropped, a partial chart can be found here. Using this code, it is also possible to filter a gradient color using a range of color codes in the If NOT Like statement:

Private Declare PtrSafe Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare PtrSafe Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare PtrSafe Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Function PixelTest(objPict As Object, ByVal X As Long, ByVal Y As Long) As Long
 Dim lDC As Variant
 lDC = CreateCompatibleDC(0)
 SelectObject lDC, objPict.Handle
 PixelTest = GetPixel(lDC, X, Y)
 DeleteDC lDC
End Function
Sub insert()
    Dim myDocument As Slide, fileSystem As Object, fileFolder As Object
    Dim fileItem As Object, objPict As Object, objImage As Object
    Dim i As Integer, startingPoint As Integer, endingPoint As Integer
    Dim MidPoint As Integer, filePath As String
    Dim scaleAdjustment As Single, margin As Single
    Dim importHeight As Single, importWidth As Single
    Dim xlocation As Single, yLocation As Single
    Dim restoreLayout As Boolean
    filePath = "D:\Pictures"
    Set fileSystem = CreateObject("Scripting.FileSystemObject")
    Set fileFolder = fileSystem.GetFolder(filePath)
    Set objImage = CreateObject("WIA.ImageFile")
    scaleAdjustment = 3
    importWidth = 330
    importHeight = 250
    xlocation = 390
    yLocation = 200
    For Each fileItem In fileFolder.Files
        If fileItem Like "*.PNG" Then
            i = i + 1
            On Error GoTo insertSlide
            Set myDocument = ActivePresentation.Slides(i)
            If myDocument.CustomLayout.Name = "Picture with Caption" Then
                myDocument.Layout = ppLayoutText
                restoreLayout = True
            End If
            Set preCroppedPic = myDocument.Shapes.AddPicture(FileName:=fileFolder & "\" & _
                fileItem.Name, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
                Left:=xlocation, Top:=yLocation, Width:=importWidth, Height:=importHeight)
            preCroppedPic.Export filePath & "\Temp.bmp", ppShapeFormatBMP, preCroppedPic.Width, preCroppedPic.Height, ppScaleToFit
            Set objImage = CreateObject("WIA.ImageFile")
            objImage.LoadFile filePath & "\Temp.bmp"
            Set objPict = LoadPicture(filePath & "\Temp.BMP")
            endingPoint = objImage.Width
            MidPoint = (0.5 * objImage.Height)
            For marginScan = 1 To endingPoint
                On Error Resume Next
                If Not (PixelTest(objPict, marginScan, MidPoint) Like "1677*") Then
                    margin = marginScan * scaleAdjustment
                    preCroppedPic.PictureFormat.CropLeft = margin
                    preCroppedPic.PictureFormat.CropRight = margin
                    preCroppedPic.PictureFormat.CropTop = margin
                    preCroppedPic.PictureFormat.CropBottom = margin
                    Exit For
                End If
            Next
            If restoreLayout Then
                myDocument.Layout = ppLayoutPictureWithCaption
                restoreLayout = False
            End If
        End If
    Next fileItem
    Exit Sub
insertSlide:
    Set myDocument = ActivePresentation.Slides.Add(i, ppLayoutText)
    Resume Next
End Sub

The preceding code produces the following result when importing uncropped files with a solid white border:
enter image description here enter image description here
Note how the white border has been trimmed after the import.
Also, note that there is an if statement that filters out non PNG files. If working with other types of picture files, this will have to be modified accordingly.

Justin Edwards
  • 310
  • 1
  • 4
  • 7