Loading a picture from a spreadsheet into a userform

PeterMac65

New Member
Joined
May 7, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi,
MickG wrote some code to load pictures from a spreadsheet into a userform. Worked well
But
I want to know how it can be adapted to load Shapes / msoPictures from a spreadsheet into a userform
I have users that are sniping and pasting pictures into a "sheet2" and i want to show all those pictures. I think the type is an msoPicture but cant be sure

Regards PeterMac

MicG said:
Your Userform should have an "Image Control" (Image1) and a "ListBox" (ListBox1).
The pictures are assumed to be on sheet2 (Change Sheet name in code as required,(2 places, sub "Pict", and "Initialize")
When you run the code the first image name in your listbox will be loaded.
After that it will change based on the Listbox selection.

Place the code below in the Userfrom Module:-
Code:
Option Explicit
Private Sub ListBox1_Click()
Call Pict(ListBox1.ListIndex)
End Sub


Private Sub UserForm_Initialize()
Dim Pic As Object
For Each Pic In Sheets("Sheet2").Pictures
If TypeName(Pic) = "Picture" Then
ListBox1.AddItem Pic.Name
End If
Next Pic
ListBox1.ListIndex = 0
Call Pict(0)
End Sub


Sub Pict(n)
Dim Ans As String
Dim rng As Excel.Range
Dim cht As Excel.ChartObject
Dim Pth As String
Dim Pic As Object
With ListBox1
Set Pic = Sheets("sheet2").Shapes(.List(n))
Pic.Copy 'Picture xlScreen, xlBitmap
Dim Strpath As String
Strpath = ThisWorkbook.Path & "\Temp.jpg"
Set cht = ActiveSheet.ChartObjects.Add(100, 0, Pic.Width, Pic.Height)
cht.Chart.Paste
cht.Chart.Export Strpath
cht.Delete
Set cht = Nothing
Me.Image1.Picture = LoadPicture(Strpath)
End With
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Attachments

  • Temp.jpg
    Temp.jpg
    8 KB · Views: 34
Upvote 0
Hey, i tried this code, what im doing is downloading images from Internet with the next code:
VBA Code:
    Set img = Sheets("ship").Pictures.Insert([f10].Value)
EX: https://upload.wikimedia.org/wikipedia/commons/c/cd/USS_Pennsy_BB-38_1934.jpg

but when i run MicG´s code, the image is in blank. Look the attached, do you know how to fix that?
i solve the situation like this:
VBA Code:
'rs![SHIP IMAGE].Value = (URL link as string)
Set img = Sheets("ship").Pictures.Insert(rs![SHIP IMAGE].Value)

Dim Ans As String
Dim rng As Excel.Range
Dim cht As Excel.ChartObject
Dim Pth As String
Dim Pic As Object
With main.ListBox1
Set Pic = Sheets("ship").Pictures("img_SHIP")
Pic.Copy 'Picture xlScreen, xlBitmap
Dim Strpath As String
Strpath = ThisWorkbook.Path & "\Temp.jpg"
Set cht = Sheets("ship").ChartObjects.Add(100, 0, Pic.Width, Pic.Height)
cht.Chart.Paste
cht.Chart.Export Strpath
Application.Wait (10)
cht.Delete
Set cht = Nothing
main.Image9.Picture = LoadPicture(Strpath)
main.Image9.PictureSizeMode = fmPictureSizeModeStretch

but now the issue is the delay between the image download and the chart is created, how can i make the code wait until download the image?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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