I'm trying to write a script that will allow me to load pictures contained in my workbook into my userform dynamically in an attempt to make the workbook completely portable. I've come up with the following that seems to work but there is one line which I don't understand why it doesn't work without. If I remove the line .ChartArea.Select the image won't load. However, If I leave it in it works fine. Ideally I'd like to remove it so I can avoid using a pointless Select. Can anyone explain?
Option Explicit
Private Sub UserForm_Initialize()
Me.Picture = LoadPicture(Filename:=ExportMyPicture(Sheet1.Pictures(1)))
Me.PictureSizeMode = fmPictureSizeModeZoom
End Sub
Private Function ExportMyPicture(pic As Picture) As String
Dim fName As String
fName = Environ("Temp") & "/" & pic.Name & ".bmp"
With pic.Parent.ChartObjects.Add(50, 40, pic.ShapeRange.Width, pic.ShapeRange.Height)
.Border.LineStyle = 0
pic.Copy
With .Chart
' Removing the following line stops the picture from loading
.ChartArea.Select
.Paste
If .Export(Filename:=fName, filtername:="bmp") Then
ExportMyPicture = fName
End If
End With
.Delete
End With
End Function
Demo:
Using this png:
url: SO converts it to a jpg
http://pngimg.com/uploads/cat/cat_PNG50497.png
Picture by Mikku


.ChartArea.Paste?