VBA 将图像从工作表复制到用户表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45014913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Copy image from worksheet to Userform
提问by Sphinx
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"
有没有办法在运行时将图像从工作表复制到用户表单图像控件?
我在包含图像的工作表上得到了一个形状。当我选择 --> 复制 (ctrl + C) 这个形状时,转到UserForm1
设计 -->image1
属性我可以在图片属性中执行 ctrl + vimage1
并将图像从剪贴板粘贴到image1
控件。
如何在运行时使用 VBA 实现这一点?
我试过UserForm1.Image1.Picture = ActiveSheet.Shapes("Picture 1").Picture
很多类似的机器人都没有工作
我通常会得到“对象不支持此属性或方法”或“类型不匹配”
采纳答案by Vityata
Some time ago I was looking for a solution of the same problem. Did not find a solution, but I found a great workaround:
前段时间我正在寻找同样问题的解决方案。没有找到解决方案,但我找到了一个很好的解决方法:
Make a separate form with plenty of pictures in it. Name it
user_form_pics
.Then call the following on your form:
制作一个单独的表格,里面有很多图片。命名它
user_form_pics
。然后在您的表单上调用以下内容:
Me.Image1.Picture = user_form_pics.img_name11.Picture
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:
编辑:如果您需要将Chart保存为图片,步骤如下:
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