Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel 2010 - Embedding Pictures and Resizing

Tags:

excel

vba

I've been lurking for a while and found it very helpful, so thanks for the help already!

I'm trying to write a macro to embed images into a worksheet from individual files and resize them, whilst keeping the full resolution of the image intact should it need to be enlarged again. First of all I tried:

ActiveSheet.Pictures.Insert(imageName).Select
With Selection.ShapeRange
    .Height = 100
    .Width = 100
End With

This essentially inserted a link to the picture and if the image file was removed or the excel file moved to another computer, the link would be broken. Next I tried:

ActiveSheet.Shapes.AddPicture Filename:=imageName, _
    linktofile:=msoFalse, _
    savewithdocument:=msoCTrue, _
    Width:=100, _
    Height:=100

This code also works, but the image is resized to 100 * 100 pixels before insertion, so the original file resolution is lost.

Is there any way to insert image files and then scale them down in size, so that the original resolution is retained?

like image 800
awenborn Avatar asked Feb 27 '14 15:02

awenborn


People also ask

How do I resize an image in Excel VBA?

Step 1: Insert the pictures in to a worksheet, and select a picture that you will resize it to fit a single cell. Step 2: Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window. Step 3: Click Insert > Module, and paste the following macro in the Module Window.

How do I paste a range of cells into a message body as an image in Excel?

Note: If you need to paste multiple ranges from different worksheets, the below VBA code can do you a favor: First, you should select the multiple ranges that you want to insert into the email body as pictures, and then apply the following code: VBA code: paste multiple ranges of cells into email body as image: Sub ...


Video Answer


1 Answers

You first load and position the picture in its original size, and in a second step resize it as desired. You only specify EITHER width or heigth to retain the aspect ratio.

Sub Test()
Dim MySht As Worksheet
Dim MyPic As Shape
Dim MyLeft As Single, MyTop As Single

    ' position in Pixel relative to top/left of sheet
    MyTop = 50
    MyLeft = 50

    ' alternatively position to the top/left of [range] C3
    MyTop = [C3].Top
    MyLeft = [C3].Left

    ' alternatively position to top/left of actual scrolled position
    MyTop = Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn).Top
    MyLeft = Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn).Left


    Set MySht = ActiveSheet
    Set MyPic = MySht.Shapes.AddPicture("C:\Users\MikeD\Desktop\Untitled.png", _
                msoFalse, msoTrue, MyLeft, MyTop, -1, -1)
    '      ^^^  LinkTo    SaveWith                -1 = keep size

    ' now resize pic
    MyPic.Height = 100

End Sub

... and try to avoid .Select ... Dim the objects you need and use them.

like image 156
MikeD Avatar answered Oct 03 '22 16:10

MikeD