VBA Code - Remove link to inserted pictures

Spaztic

New Member
Joined
Jul 27, 2023
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I have VBA code (see below) that inserts a picture. The issue is that when I send the Excel file to someone else, the pictures do not show up and they get the error in place of the picture (see below image) when they open the Excel file. Is there something that can be put in the VBA code that 'de-links' the image from my directory?

Appreciate any help you can give me!


1714397745844.png


VBA Code:
Sub InsertPic()

'Make all pictures move and size with cell
    Dim xPic As Picture
    On Error Resume Next
    Application.ScreenUpdating = False
    For Each xPic In ActiveSheet.Pictures
        xPic.Placement = xlMoveAndSize
    Next
    Application.ScreenUpdating = True


'Add picture...centered...scaled
    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Picture
  
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
      
    If fNameAndPath = False Then Exit Sub
  
    Set rng = ActiveCell
  
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.7
        Else
            .Height = rng.Height * 0.7
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
  
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
It's likely due to a bug that exists with Pictures.Insert. Therefore, use the AddPicture method of the Shapes object instead. I've amended your code accordingly. You'll notice that img is now declared as Shape.

VBA Code:
Sub InsertPic()

'Make all pictures move and size with cell
    Dim xPic As Picture
    On Error Resume Next
    Application.ScreenUpdating = False
    For Each xPic In ActiveSheet.Pictures
        xPic.Placement = xlMoveAndSize
    Next
    Application.ScreenUpdating = True


'Add picture...centered...scaled
    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Shape
 
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
      
    If fNameAndPath = False Then Exit Sub
 
    Set rng = ActiveCell
 
    Set img = ActiveSheet.Shapes.AddPicture(Filename:=fNameAndPath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.7
        Else
            .Height = rng.Height * 0.7
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
 
End Sub

Hope this helps!
 
Upvote 0
Solution
This did the trick. Thank you for the quick answer as well!
 
Upvote 0
It's likely due to a bug that exists with Pictures.Insert. Therefore, use the AddPicture method of the Shapes object instead.
Let me explain a bit.
Pictures.Insert creates a sub-linked image (to reduce the weight of the XLS file), while AddPicture embeds the image in the document (which of course increases the file weight by the weight of the image).

Artik
 
Upvote 0
Hi @Artik ,

Thanks for your comments.

Actually, in Excel 2007, it actually embeds the picture within the workbook. But, for some reason, in Excel 2010, its behaviour changed. As you've already mentioned, it links the image instead. It was my understanding at the time that it was a bug. But I may be mistaken.

In any case, since Pictures.Insert has been deprecated, I use Shapes.AddPicture instead, which as you know allows one to explicitly specify whether to embed or link the image.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,216,022
Messages
6,128,324
Members
449,440
Latest member
Gillian McGovern

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top