Excel VBA:将图片从图像控件复制到 ActiveX 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7719450/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:13:25  来源:igfitidea点击:

Excel VBA: Copying Pictures from image controls to activeX objects

excelimagevbaexcel-vbaactivex

提问by Surferosa

I have a series of images that I need to display multiple times on both forms (via image controls) and on worksheets (via activeX image controls). I know that I could keep the files externally and use the loadpicturemethod; but there is something I want to avoid if possible.

我有一系列图像需要在表单(通过图像控件)和工作表(通过 activeX 图像控件)上多次显示。我知道我可以将文件保存在外部并使用该loadpicture方法;但如果可能的话,我想避免一些事情。

I also know I could save and load- but again I would rather not use an external file write to perform the task. Ideally, everything will stay embedded and hidden within the file itself.

我也知道我可以保存和加载 - 但我还是不想使用外部文件写入来执行任务。理想情况下,所有内容都将嵌入并隐藏在文件本身中。

I think there maybe a solution in using the clipboard- but I couldn't get the syntax to work. The object is embedded always in the same location(s); it never moves or changes size or other properties (beyond .visible). So what I would really like to do is something simple like;

我认为使用剪贴板可能有一个解决方案 - 但我无法使语法起作用。对象总是嵌入在相同的位置;它永远不会移动或更改大小或其他属性(超出 .visible)。所以我真正想做的是一些简单的事情;

Sheet1.oleobjects("toImage").object.picture = frm1.fromImage.picture

**Edit: **
I think I've found a solution to this; but still have a related question.

**编辑:**
我想我已经找到了解决方案;但仍然有一个相关的问题。

I worked out that I could do what I want if I embed a series of activeX images on a sheet; then reference them in the actual controls / objects I want. So;

我发现如果我在一张纸上嵌入一系列 activeX 图像,我可以做我想做的事;然后在我想要的实际控件/对象中引用它们。所以;

Sheet1.oleobjects("toImage").object.picture=Sheet1.oleobjects("FromImage").object.picture

or

或者

frm1.Controls("toImage").picture = Sheet1.oleobjects("FromImage").object.picture

But, the below doesn't work when I try to do the same using an inserted picture (a shape object);

但是,当我尝试使用插入的图片(形状对象)执行相同操作时,以下内容不起作用;

frm1.toImage.picture =  sheet1.shape("FromImage").picture

..isn't valid syntax. It seems the only thing I can do with them is copy them- I couldn't use them to set the picture of another object without using the clipboard.

.. 不是有效的语法。似乎我唯一能做的就是复制它们 - 我无法在不使用剪贴板的情况下使用它们来设置另一个对象的图片。

The solution above works for me (using a series of activeX image objects rather than pictures)- but I am curious why I can't do with using a standard picture (shape).

上面的解决方案对我有用(使用一系列 activeX 图像对象而不是图片)-但我很好奇为什么我不能使用标准图片(形状)。

回答by shA.t

If you want to add a picture to your Excel sheet use something like this:
(You can change ActiveSheetto your favorite sheet)

如果您想在 Excel 工作表中添加图片,请使用以下内容:(
您可以更改ActiveSheet为您最喜欢的工作表)

Dim aSheet As Worksheet
Dim aShape As Shape
Set aSheet = ActiveSheet
Set aShape = aSheet.Shapes.AddPicture("<FileName>", msoFalse, msoTrue, 120, 120, 200, 200)

And for more details :

有关更多详细信息:

Function AddPicture(Filename As String, LinkToFile As MsoTriState, SaveWithDocument As MsoTriState, Left As Single, Top As Single, Width As Single, Height As Single) As Shape

Function AddPicture2(Filename As String, LinkToFile As MsoTriState, SaveWithDocument As MsoTriState, Left As Single, Top As Single, Width As Single, Height As Single, Compress As MsoPictureCompress) As Shape

函数 AddPicture(文件名为字符串,LinkToFile 为 MsoTriState,SaveWithDocument 为 MsoTriState,左为单一,顶部为单一,宽度为单一,高度为单一)作为形状

函数 AddPicture2(文件名为字符串,LinkToFile 为 MsoTriState,SaveWithDocument 为 MsoTriState,左为单一,顶部为单一,宽度为单一,高度为单一,压缩为 MsoPictureCompress)为形状



And if you want to load a picture to your Imagecomponent on your form :
(Add an Imagecomponent to your form [: Image1])

如果您想将图片加载到Image表单上
Image组件中:(向表单中添加一个组件 [: Image1])

Set Image1.Picture = LoadPicture("<FileName>")