Forum Discussion

Arnaud_Guitton's avatar
Arnaud_Guitton
Copper Contributor
Jul 04, 2024

How to know the original path of an inserted image ?

Hi all,
I have a problem with an Excel file. In this file I used the feature "Insert image above cell" and I chose an image from a local path (on my hard drive). So far so good. 

However, if I copy the file to another computer, the image is gone. That's the concept.


Now the problem is that this an auto-generated Excel file from an external program. I know where the images are but I generated multiple reports, modified some of the images and changed the images in some excel files to point to other folders. It works fine on the original computer but I copied the Excel files and the images (or so I thought) to the other computer and some images are missing.

The question is : how do I know the original path of the image so that I can also copy it and re-link it.

 

The additionnal question to that is : I modified the local image for some reason (didn't change the path) and I want to update the shown image to include my modification without closing and opening again the Excel file. Is that possible and how ? But the first question is really  more important because basically I have a new laptop and I need to recover the whole file and images to export it if needed later in time when I won't have the old laptop anymore.

3 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Arnaud_Guitton 

    To determine the original path of an inserted image in an Excel file and to update the displayed image without closing and reopening the Excel file, you can use VBA. Here's how to achieve both:

    1. Determine the Original Path of an Inserted Image

    Unfortunately, Excel does not store the original file path of an inserted image. Once the image is embedded, the path information is not retained. However, if the images were linked (not embedded), the path information can be accessed.

    If the images were embedded directly and the original paths were not saved in some way, there's no direct method to retrieve those paths through VBA or any built-in Excel feature. It would be advisable in future to keep a log of image paths if they need to be referenced later.

    1. Update the Displayed Image without Closing and Reopening the Excel File

    You can use VBA to update an image in an Excel file. Here’s a step-by-step guide to do this:

    Step 1: Open the VBA Editor

    1. Press Alt + F11 to open the VBA Editor.

    Step 2: Insert a New Module

    1. Go to Insert > Module.

    Step 3: Copy and Paste the Following VBA Code

    This code will replace an existing image with a new image:

    Vba Code is untested backup your file.

     

     

     

    Sub UpdateImage()
        Dim ws As Worksheet
        Dim img As Shape
        Dim imgPath As String
        Dim newImg As Shape
        Dim topPos As Single
        Dim leftPos As Single
    
        ' Set the worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
    
        ' Specify the path of the new image
        imgPath = "C:\path\to\your\new\image.jpg" ' Change this to your image path
    
        ' Loop through all shapes in the worksheet
        For Each img In ws.Shapes
            If img.Type = msoPicture Then
                ' Get the position of the existing image
                topPos = img.Top
                leftPos = img.Left
                
                ' Delete the existing image
                img.Delete
    
                ' Insert the new image at the same position
                Set newImg = ws.Pictures.Insert(imgPath)
                newImg.Top = topPos
                newImg.Left = leftPos
                
                ' Optional: adjust the size of the new image to match the old one
                'newImg.Width = img.Width
                'newImg.Height = img.Height
    
                Exit For ' Assuming there's only one image to replace
            End If
        Next img
    
        MsgBox "Image updated successfully!"
    End Sub

     

     

     

    Step 4: Run the Macro

    1. Close the VBA Editor (click the X or press Alt + Q).
    2. Press Alt + F8 to open the Macro dialog box.
    3. Select UpdateImage and click Run.

    Notes:

    • File Paths: Ensure the imgPath is correct and points to the new image you want to insert.
    • Sheet Name: Change "Sheet1" to the name of the worksheet where your image is located.
    • Position and Size: The new image will be placed at the same position as the old one. Uncomment and adjust the size lines if you need the new image to match the size of the old image.

    Additional Considerations:

    • Logging Paths: For future reports, consider logging the original paths of images in a hidden worksheet or a metadata section within your workbook.
    • Linked Images: If you plan to move workbooks and need to retain image paths, consider linking images rather than embedding them. Linked images retain their file paths and can be updated if the paths remain valid on the new system. However, linked images require that the path remains accessible and unchanged.

    By following these steps, you can manage your images more effectively in Excel and ensure they remain updated and correctly linked when moving files between different systems.

     

    Excel Add-ins and other optons

    There are third-party Excel add-ins that might help manage and update images.

    Or using Power Query, Workflows, Built-In Features, etc.

     

    Conclusion

    While VBA provides a powerful and automated way to handle images in Excel, there are several other methods you can consider based on your comfort level with coding, the complexity of your task, and the frequency with which you need to update images. Whether it's manual management, using add-ins, or linking images, choose the method that best fits your workflow and requirements.

    The text, steps and code were created with the help of AI.

     

    *I also add this file that I found in my old archive, created by Karin (Beverly) http://Excel-Inn.de

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

    • Arnaud_Guitton's avatar
      Arnaud_Guitton
      Copper Contributor

      Hi, sorry I completely missed you answer last year (I used my personal acount and I almost never go check my mails there and as I was in a hurry and found a workaround I never came back here either).
      I found another solution where I needed to basically open the Excel file as a text file (but first I had to access it with 7zip or something like that) and then I could see the links. I'm sorry I don't remember the exact procedure and I don't think I kept the link (because sometimes I'm so stupid...).

      Your macro won't help me in this case but I appreciate anyway.

      You rote :

      • Linked Images: If you plan to move workbooks and need to retain image paths, consider linking images rather than embedding them. Linked images retain their file paths and can be updated if the paths remain valid on the new system. However, linked images require that the path remains accessible and unchanged.

      My problem is here at the beginning, when I insert an image in a cell using "Insert > Image > inside cell > from this computer" it creates a linked image automatically (but at the time I didn't know that) so when I changed my laptop and moved my files (or shared the Excel file with someone else) I had a problem with the linked images.
      However I don't know how to change the behaviour of Excel so that when I insert an image from my computer it creates an embedded image instead of a linked image. In my case I need that because I need to send the Excel file to other people and I don't want to send a full ZIP file with the images contained in the Excel file.

      I can't believe that Microsoft didn't do a smart function to :
      1- Better manage linked images and update them
      2- Know where the images comes from when the link is broken.

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        When you use Insert > Picture > From This Device > Insert in Cell in Excel:

        • Excel sometimes embeds images, but
        • often, it links them if you insert the image as a "linked image", or depending on the method used in the underlying API.
        • The problem is: Excel does not make it obvious whether the image is embedded or linked.
        • And yes — Microsoft has no proper UI to manage image links, unlike how it manages formula links (Data > Edit Links for formulas but not for images!).

        About your discovery: extracting with 7-Zip

        You were absolutely right! Excel .xlsx files are actually ZIP archives internally.

        If you:

        • Change the file extension from .xlsx to .zip OR
        • Open with 7-Zip, WinRAR, or similar,

        You will find this structure inside:

        /xl/

          /media/    <-- embedded images are here

          /drawings/ <-- links to images are managed here

          /worksheets/  <-- drawings are attached to sheets

        If your images were embedded, you would see them in /xl/media/ as files like image1.png, image2.jpeg, etc.

        If your images were linked, you would find the link path written in XML files like:

        • xl/drawings/_rels/drawing1.xml.rels
        • and in xl/drawings/drawing1.xml
        • or even in xl/worksheets/_rels/sheet1.xml.rels.

        There you could read the original absolute file paths you used on your old computer (like C:\Users\you\Pictures\mychart.png).

        That's how you can recover the original image paths.

        How to make Excel embed images instead of linking

        Sadly, Excel itself does not give you a checkbox like "embed or link" when inserting a picture.

        BUT here’s what you can do to force embedding:

        Method

        Behavior

        How to Ensure Embedding

        Insert via UI (Insert > Pictures > This Device)

        Depends

        Immediately after inserting, copy the picture (Ctrl+C) and paste it again (Ctrl+V). The pasted image becomes embedded.

        Insert via VBA

        Full Control

        If you use VBA Shapes.AddPicture, you can set LinkToFile:=False, so it embeds.

        Copy-Paste from another app (e.g., Paint)

        Always Embedded

        Copy from outside Excel (like Paint) and paste.

        So, if you want guaranteed embedded images without VBA, an easy trick is:

        • Insert normally
        • Immediately copy the inserted image (Ctrl+C)
        • Paste it again (Ctrl+V)
        • Delete the original
        • The pasted version is now embedded.

        It’s stupid simple… but it works.

         

        Short VBA Trick to Embed All Linked Images

        If you already have many linked images, you can automatically re-embed them with VBA:

        Sub ListLinkedImages()
            Dim ws As Worksheet
            Dim shp As Shape
            Dim reportWs As Worksheet
            Dim reportRow As Long
        
            On Error Resume Next
            Set reportWs = ThisWorkbook.Worksheets("Image Links Report")
            If reportWs Is Nothing Then
                Set reportWs = ThisWorkbook.Worksheets.Add
                reportWs.Name = "Image Links Report"
            Else
                reportWs.Cells.Clear
            End If
            On Error GoTo 0
        
            reportWs.Cells(1, 1).Value = "Sheet Name"
            reportWs.Cells(1, 2).Value = "Image Name"
            reportWs.Cells(1, 3).Value = "Linked File Path"
        
            reportRow = 2
        
            For Each ws In ThisWorkbook.Worksheets
                For Each shp In ws.Shapes
                    If shp.Type = msoLinkedPicture Then
                        reportWs.Cells(reportRow, 1).Value = ws.Name
                        reportWs.Cells(reportRow, 2).Value = shp.Name
                        reportWs.Cells(reportRow, 3).Value = shp.LinkFormat.SourceFullName
                        reportRow = reportRow + 1
                    End If
                Next shp
            Next ws
        
            MsgBox "Done! Found " & reportRow - 2 & " linked images.", vbInformation
        End Sub
        'Code is Untestet, backup your file first.
        • It copies every linked picture,
        • Pastes it back (as embedded),
        • Deletes the original linked one.

        Super useful before sending your file to others!

        Final thoughts

        • Microsoft really should have given us better image management tools.
        • You found the "archive and search the XML" trick, which is the real hardcore way.
        • If you're rebuilding your reports for portability, I recommend embedding images before you send or backup the file.  Hope it helps 🙂

Resources