How to embed a linked picture in Excel

Visual Basic Topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to embed a linked picture in Excel

Post by Neo » Mon Aug 26, 2019 1:25 am

After moving your Excel file to a different location, if you find your images were linked and not embedded, here is a solution to embed those.

First move the file back the location where images work. Open the Excel file and you should be able to see the images ok.

Now open VBA and put following code. It will embed the picture and delete the linked image.

Code: Select all

Sub UnlinkImage()

    For Each Shape In ActiveSheet.Shapes
        If Shape.TopLeftCell.Column = 5 Then
            Shape.Copy
            Range("E" & Shape.TopLeftCell.Row).Select
            ActiveSheet.PasteSpecial Format:="Picture (PNG)", Link:=False, DisplayAsIcon:=False
            Set c = Cells(Shape.TopLeftCell.Row, "E")
            Selection.Left = c.Left + c.Width / 2 - Selection.Width / 2
            Selection.Top = c.Top + c.Height / 2 - Selection.Height / 2
            Shape.Delete ' Delete original image
        End If
    Next Shape

End Sub

Post Reply

Return to “Visual Basic Programming”