Add photo in excel from local folder

0

Hello,

I followed the steps all the steps here - Add photo in excel worksheet from local folder And everything worked fine.

But I wanted to resize the images and align them to the center of the cell - horizontal and vertical.

As I did not know how to do it, I edited like this: oCell.Width / 1.2, oCell.Height / 1.2

The image is the size you wanted in relation to the cell, but always stays aligned on the top left. How do I now align the image to the center of the cell?

    
asked by anonymous 24.11.2016 / 19:24

1 answer

2

When you say "edited" I'm assuming you've changed this code snippet:

With oImage
    .Left = oCell.Left
    .Top = oCell.Top
    .Width = oCell.Width
    .Height = oCell.Height
End With

For this:

With oImage
    .Left = oCell.Left
    .Top = oCell.Top
    .Width = oCell.Width / 1.2
    .Height = oCell.Height / 1.2
End With

That is, in practice you have reduced the size of the image. So, to center the image in the cell (without worrying about how much factor has been applied), the easiest way is to add to the top coordinates ( Left and Top ) half the cell size and decrease half the image size (which then needs to be calculated before , depending on the factor you are applying). Try it then:

factor = 1.2 ' Separei em uma variável só pra deixar claro que você pode
             ' mudar sem precisar mexer nas duas linhas finais.
With oImage
    .Width = oCell.Width / factor
    .Height = oCell.Height / factor
    .Left = oCell.Left + oCell.Width / 2 - .Width / 2
    .Top = oCell.Top + oCell.Height / 2 - .Height / 2
End With
    
24.11.2016 / 19:55