Display embedded image on userform

YasserKhalil
PlatinumLounger
Posts: 4967
Joined: 31 Aug 2016, 09:02

Display embedded image on userform

Post by YasserKhalil »

Hello everyone
I have inserted picture from Insert >> Pictures >> Place in Cell >> This Device
In the userform there is an image control where I would like to display the embedded image in cell A1 to the userform.

Dave
NewLounger
Posts: 14
Joined: 06 Jun 2024, 12:36

Re: Display embedded image on userform

Post by Dave »

Hi YasserKhalil. If you have the picture file you can just load it to the userform image control. If you want to display the embedded image without using the picture file, I think you may need to use a chart to copy the image to. Then export the picture file from the chart which you can then load into the userform image control... seems redundant when you already have the picture file. Dave

Dave
NewLounger
Posts: 14
Joined: 06 Jun 2024, 12:36

Re: Display embedded image on userform

Post by Dave »

Here's some code to make your ws pic into a .jpg file which you can then load to your userform image control. HTH. Dave
Userform code...

Code: Select all

Dim PicFile As String
Sub test()
'pics in Sheet1
Dim MyChart As Chart, sh As Shape
On Error GoTo below
Application.ScreenUpdating = False
'add temp chart
Charts.Add.Location Where:=xlLocationAsObject, Name:="Sheet1"
Sheets("Sheet1").ChartObjects(Sheets("Sheet1").ChartObjects.Count).Name = "MYChart"
With Sheets("Sheet1")
For Each sh In .Shapes
If sh.Type = msoPicture Or sh.Type = msoLinkedPicture Then
'make jpg file
Call CreateJPG(sh.Name, sh)
Exit For
End If
Next sh
End With
'remove temp chart
Sheets("Sheet1").ChartObjects("MYChart").Delete
below:
Application.ScreenUpdating = True
If Err.Number <> 0 Then
MsgBox "Error"
End If
End Sub

Sub CreateJPG(PicName As String, Shp As Shape)
'make image files
'picname is XL picture name; Shp is pic
Dim xRgPic As Shape
ThisWorkbook.Worksheets("Sheet1").Activate
Set xRgPic = ThisWorkbook.Worksheets("Sheet1").Shapes(PicName)
xRgPic.CopyPicture
With Sheets("Sheet1").ChartObjects("MYChart").Chart
'size chart to Rng
.Parent.Height = Shp.Height
.Parent.Width = Shp.Width
.Parent.Top = Shp.Top
.Parent.Left = Shp.Left
End With
'make file in wb path
With Sheets("Sheet1").ChartObjects("MYChart")
.Activate
.Chart.Paste
.Chart.Export ThisWorkbook.Path & "\" & PicName & ".jpg", "JPG"
End With
'make file name
PicFile = ThisWorkbook.Path & "\" & PicName & ".jpg"
End Sub
To operate...

Code: Select all

'run the code
Call test
'display pic in userform
UserForm1.Image1.Picture = LoadPicture(PicFile)
'remove pic file
Kill PicFile

YasserKhalil
PlatinumLounger
Posts: 4967
Joined: 31 Aug 2016, 09:02

Re: Display embedded image on userform

Post by YasserKhalil »

Thanks a lot. But this doesn't work for my case. I have inserted the image by (Place in Cell) command.

Dave
NewLounger
Posts: 14
Joined: 06 Jun 2024, 12:36

Re: Display embedded image on userform

Post by Dave »

"doesn't work" ??? Not sure what this means? The code errors or doesn't do anything? The code trial worked for me. I don't think it matters how you insert the pic or whether you place in cell. I did forget to mention that you need to adjust the code for sheet name, userform name, and userform image control name. The operate code also has to be userform code. Dave

YasserKhalil
PlatinumLounger
Posts: 4967
Joined: 31 Aug 2016, 09:02

Re: Display embedded image on userform

Post by YasserKhalil »

The picture insertion is different from normal insert. I used the command from office 2024, Place in Cell and it is different from the normal as I used to get in the previous versions. I tried the normal approach and your way is perfect and working.

Dave
NewLounger
Posts: 14
Joined: 06 Jun 2024, 12:36

Re: Display embedded image on userform

Post by Dave »

Sorry YasserKhalil but I don't have a copy of office 2024. Hopefully others may be able to help you out. Dave