简体   繁体   中英

VBA Copy image from worksheet to Userform

Is there a way to runtime copy image from worksheet to userform image control?
I got a shape on a worksheet containing image. And when I select --> copy (ctrl + C) this shape, go to the UserForm1 design --> image1 properties I can do ctrl + v in the picture property of image1 and the image is pasted from clipboard to image1 control.
How can I achieve this using VBA in runtime?
I tried UserForm1.Image1.Picture = ActiveSheet.Shapes("Picture 1").Picture And many similar bot none of them work
I usually get "Object doesn't support this property or method" or "Type mismatch"

Some time ago I was looking for a solution of the same problem. Did not find a solution, but I found a great workaround:

  1. Make a separate form with plenty of pictures in it. Name it user_form_pics .

  2. Then call the following on your form:

Me.Image1.Picture = user_form_pics.img_name11.Picture

This is 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 the picture of the user_form_pics.img_name11

Edit: In case that you need to save Chart to picture, the procedure is the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM