PeterMac65
New Member
- Joined
- May 7, 2020
- Messages
- 21
- Office Version
- 365
- Platform
- 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
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)
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
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
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)
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