简体   繁体   English

VBA将图像从工作表复制到Userform

[英]VBA Copy image from worksheet to Userform

Is there a way to runtime copy image from worksheet to userform image control? 有没有办法将图像从工作表复制到userform图像控件?
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. 当我选择 - >复制(ctrl + C)这个形状时,转到UserForm1设计 - > image1属性我可以在image1的picture属性中执行ctrl + v,并将图像从剪贴板粘贴到image1控件。
How can I achieve this using VBA in runtime? 如何在运行时使用VBA实现此目的?
I tried UserForm1.Image1.Picture = ActiveSheet.Shapes("Picture 1").Picture And many similar bot none of them work 我试过UserForm1.Image1.Picture = ActiveSheet.Shapes("Picture 1").Picture和许多类似的机器人都没有工作
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 . 将其命名为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 现在,您的表单中包含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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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