Excel 2010 - Embed Image from Macro

I have a macro someone else wrote back when Excel 2007 was common.  It worked in 2007, however, it only links the images in Excel 2010.  Can someone help me adjust the code below to embed images in Excel 2010.

Sub InsertPicture()
Dim myPicture As Variant
ActiveSheet.Unprotect
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = False Then Exit Sub
Application.ScreenUpdating = False
Dim iLeft#, iTop#, iWidth#, iHeight#
With Range("A37")
iLeft = .Left
iTop = .Top
.Select
End With


With ActiveSheet.Pictures.Insert(myPicture)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = Range("A37").Width
        .Height = Range("A37").Height
    End With
    .Placement = 1
    .PrintObject = True
End With


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
ActiveSheet.Protect
End Sub

Open in new window

Russell KozoraManager-IS/IT Support SrevicesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
I just checked the code and it did insert the images even in excel 2010..

Try checking cell a37 that's where the images are getting embed

Enclosed is the workbook for your reference...

Saurabh...
Image.xlsm
Michael FowlerSolutions ConsultantCommented:
Just ran it in Excel 2013 64bit and it worked fine for me too. While I was at it I cleaned it up a little too

Sub InsertPicture()
    Dim myPicture As Variant
    
    myPicture = Application.GetOpenFilename _
        ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
    If myPicture = False Then Exit Sub
    
    ActiveSheet.Unprotect
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Range("A37").Select

    With ActiveSheet.Pictures.Insert(myPicture)
        With .ShapeRange
            .LockAspectRatio = msoTrue
            .Width = Range("A37").Width
            .Height = Range("A37").Height
        End With
        .Placement = 1
        .PrintObject = True
    End With

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    ActiveSheet.Protect
End Sub

Open in new window

Russell KozoraManager-IS/IT Support SrevicesAuthor Commented:
The code works to insert a Link to an image, however I need to embed the image.  If you move the image location, or transfer the excel sheet to another computer, the image is lost.  Showing the following message:

LostImage.png
Russell KozoraManager-IS/IT Support SrevicesAuthor Commented:
After a bit more searching, I was able to find another article that had the solution.

Here is the final code:

Sub InsertPicture()
    Dim myPicture As Variant
    Dim myCell As Range
    Dim MyPic As Shape
        
    myPicture = Application.GetOpenFilename _
        ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
    If myPicture = False Then Exit Sub
    
    ActiveSheet.Unprotect
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Set myCell = Range("A37")
    myCell.Select
   
    Set MyPic = ActiveSheet.Shapes.AddPicture(Filename:=myPicture, linktofile:=msoFalse, _
        savewithdocument:=msoCTrue, Left:=myCell.Left, Top:=myCell.Top, Width:=-1, Height:=-1)
    With MyPic
        .LockAspectRatio = msoCTrue
        .Width = myCell.Width
        .Height = myCell.Height
    End With
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    ActiveSheet.Protect
End Sub

Open in new window


The answer was to insert the image as a shape.  I can then take the shape and re-size it, maintaining the aspect ratio.

Thanks for the assist!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russell KozoraManager-IS/IT Support SrevicesAuthor Commented:
The solution builds on the cleaned up macro provided by the Experts.  It does resolve the issue where a linked image wasn't getting embedded.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.