0

Since a while I am trying to build a code to insert pictures in a cell in Excel and the result until now was very good. Thanks to several post on this webpage like:

Inserting picture using macro vba to adopt to a merged cell or single cell

VBA inserting picture into specific column of the table

How to insert a picture into Excel at a specified cell position with VBA

Insert picture into Excel and keep aspect ratio without exceeding dimensions with VBA

Get the size of a picture

What I found is that, it does not work when I try to adapt a picture with vertical format 4:3 or 16:9. The height of the photo is bigger than the height from the cell.

Also when I get the dimensions of the picture directly with VBA, the result of the code is that the width is bigger than the high. But, and here comes the interesting part, if I cut the photo only a bit it will work like usual. The code will work and the dimensions are right.

Somehow in those formats 4:3 or 16:9, and when the format is vertical, Excel exchanges the dimensions of the photo. Does anyone know why something like this could happen?

Update: Here is the code that I am using plus a link for one of the picture.

    Sub Pictures()
      
    Dim wb As Workbook
    Set wb = ActiveWorkbook
        
    counter = 0
    
        strCompFilePath = wb.Sheets("List").Cells(1, 1)
            If strCompFilePath <> "" Then
                counter = counter + 1
                Sheets("Template").Activate
                Sheets("Template").Range("A" & counter).RowHeight = 250
                Call Insert(strCompFilePath, counter)
            End If
                                    
    End Sub
    Function Insert(PicPath, counter)
    
        Dim l, r, t, b
        Dim w, h      ' width and height of range into which to fit the picture
        Dim aspect     ' aspect ratio of inserted picture
    
        l = 1: r = 8    ' co-ordinates of top-left cell
        t = counter: b = counter    ' co-ordinates of bottom-right cell
    
        With Sheets("Template").Pictures.Insert(PicPath)
            With .ShapeRange
                 .LockAspectRatio = msoTrue
                .Width = Range("H" & counter).Left + Range("H" & counter).Width - Range("A" & counter).Left
                .Height = Range("H" & counter).Top + Range("H" & counter).Height - Range("A" & counter).Top
                aspect = .Width / .Height     ' calculate aspect ratio of picture
                .Top = Range("A" & counter).Top + (Range("A" & counter).Height - .Height) / 2                 'left placement of picture
                .Left = Range("A" & counter).Left + Range("A:H").Left + (Range("A:H").Width - .Width) / 2     'top left placement of picture
            End With
            .Placement = 1 'Object is moved and sized with the cells
            .PrintObject = True
        End With
        
    End Function

enter image description here

enter image description here

Update: Here is the updated code thanks to @RaymonWu:

Sub Pictures()
  
Dim wb As Workbook
Set wb = ActiveWorkbook
    
counter = 5

    strCompFilePath = wb.Sheets("List").Cells(1, 1)
        If strCompFilePath <> "" Then
            counter = counter + 1
            Sheets("Template").Activate
            Sheets("Template").Range("A" & counter).RowHeight = 250
            Call Insert(strCompFilePath, counter)
        End If
                                
End Sub
Function Insert(PicPath, counter)

With Sheets("Template").Pictures.Insert(PicPath)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Height = 250
        .Top = Range("A" & counter).Top + (Range("A" & counter).Height - 
.Height) / 2                 'left placement of picture
        .Left = Range("A" & counter).Left + Range("A:H").Left + 
(Range("A:H").Width - .Width) / 2     'top left placement of picture
    End With
        .Placement = 1 'Object is moved and sized with the cells
        .PrintObject = True
    End With
    
End Function

And is the line .Height = 250 the one which is not actually working. I am starting to think that the code has no problem itself but Excel. Somehow it recognizes the width of the image as the height and vice versa.

  • 1
    Please update your question with the code attempt and perhaps some sample photos for us to reproduce the issue? – Raymond Wu Aug 06 '21 at 07:38
  • Hi @RaymondWu, thanks for answering. I could not upload the photo but here is the link: https://i.stack.imgur.com/g7B52.jpg – Alvaro Vela Aug 06 '21 at 11:28
  • I just tried to download the photo with the link to test it, and with this one work but not with the original that I had in my computer... I do not get it, how could that be? – Alvaro Vela Aug 06 '21 at 11:34
  • Hello, in this link you will see the dimensions of the picture: https://i.stack.imgur.com/5tFWk.png. How could be in a vertical photo, the width bigger than the height? Thanks. – Alvaro Vela Aug 09 '21 at 07:30
  • I can't exactly reproduce your issue since I can insert the image fine and the dimension looks correct. The first thing I notice since you posted your code is that since you are maintaining the aspect ratio of the image, why set both `Width` AND `Height` property? Setting just the `Height` would be sufficient as it will scale the other property based on the aspect ratio. (And you calculate `aspect` variable but did not use it in the entire function so what's the use of it?) – Raymond Wu Aug 09 '21 at 07:46
  • It seems that you are setting the `RowHeight` of the inserting row to `250`. If that is the final height for all images then you don't need to do the calculation - `.Height = 250` should be good enough. – Raymond Wu Aug 09 '21 at 07:51
  • Hi @RaymondWu, thanks again for your answer. If you take a look on this link, https://i.stack.imgur.com/5tFWk.png, you will see that for Excel the Width is bigger than the Height. In a vertical photo, how is that even possible? I just tried the changes that you suggested but Excel keeps exchanging the dimensions... Here is a capture of the result: https://i.stack.imgur.com/3BTVY.png. Thanks for all. – Alvaro Vela Aug 09 '21 at 08:31
  • I do see that but I'm not sure what's the cause since I can't reproduce on my end. Try stepping through line by line and observe which line causes this. It would also help to update your code in the question to your latest version. – Raymond Wu Aug 09 '21 at 08:54
  • Thanks for your answer @RaymondWu. Would be any other way that you could get the original picture? – Alvaro Vela Aug 09 '21 at 09:54
  • do you get any error? Or it just simply not giving the right dimension? – Raymond Wu Aug 09 '21 at 10:03
  • Actually not, the code runs without errors. It just not get the right dimension on the right side... – Alvaro Vela Aug 09 '21 at 10:32
  • Hi @RaymondWu, I just clicked at "Reset Picture & Size" under Picture Format > Reset Picture, and the picture not only got its original size but also rotated to the left. It is clear that for Excel this picture has an horizontal format. The problem was solved just rotating 360 degrees the original picture and saving a copy. Maybe I am able to build a VBA code to format all pictures in a folder at once. Thank you so much for all your help. – Alvaro Vela Aug 09 '21 at 11:03

0 Answers0