VBA Copy image from worksheet to Userform
Is there a way to copy an image from a worksheet to a custom image management image?
I got a shape on a sheet containing an image. And when I select -> copy (ctrl + C) this shape, go to UserForm1
design -> properties image1
which I can do ctrl + v on the image property image1
and the image will be pasted from the clipboard before image1
.
How can I achieve this using VBA at runtime?
I tried UserForm1.Image1.Picture = ActiveSheet.Shapes("Picture 1").Picture
And many similar bots none of them work Usually I get "Object does not support this property or method" or "Type mismatch"
source to share
Some time ago I was looking for a solution to the same problem. Didn't find a solution, but I found a great solution:
-
Make a separate form with lots of photos. Name it
user_form_pics
. -
Then call the following in your form:
Me.Image1.Picture = user_form_pics.img_name11.Picture
Here's how to use it in the constructor:
Private Sub UserForm_Initialize()
Me.Image1.Picture = user_form_pics.img_name11.Picture
End Sub
It works! Now your form has an imageuser_form_pics.img_name11
Edit: If you need to save a diagram for an image, the procedure is as follows:
Option Explicit
Public Sub TestMe()
Dim chtChart As Chart
Dim strPath As String
Set chtChart = ActiveSheet.ChartObjects(1).Chart
strPath = ThisWorkbook.Path & "\myChart.bmp"
chtChart.Export (strPath)
UserForm1.Show vbModeless
UserForm1.Image1.Picture = LoadPicture(strPath)
End Sub
source to share