简体   繁体   English

复制图像 - Excel VBA

[英]Copying an image - Excel VBA

SYNOPSIS- 概要-

I need to copy an image (.png format) embedded in one Excel picture object into another existing picture (overwriting its image data), while leaving intact the target shape's identity and fields (position, visibility, size, title, etc). 我需要将嵌入在一个Excel图片对象中的图像(.png格式)复制到另一个现有图片(覆盖其图像数据),同时保留目标形状的标识和字段(位置,可见性,大小,标题等)。 This is with simple pictures, not ActiveX OLEObjects. 这是简单的图片,而不是ActiveX OLEObjects。


SITUATION: 情况:

I'm on Windows 7, Excel 2010. 我在Windows 7,Excel 2010上。

I've got a sheet with multiple picture objects in the UI. 我在UI中有一张包含多个图片对象的工作表。

Depending on a user's setting, the displayed pictures need to change frequently. 根据用户的设置,显示的图片需要经常更改。 I've got the triggering and so forth all set up. 我已经触发了所有设置。

Sometimes, a single shape will need to display one of several different pictures. 有时,单个形状需要显示几个不同图片中的一个。 I'd like to just copy in the picture data from the "library" picture (embedded in another shape, hidden from the user) to that destination, but I can work around this by simply swapping in and out the picture objects. 我想将图片数据从“库”图片(嵌入另一个形状,隐藏在用户中)复制到该目的地,但我可以通过简单地交换图片对象来解决这个问题。

However, sometimes multiple shapes in the UI will need to display the same image, even while retaining their unique data (name, id, size, visibility, title, etc). 但是,有时UI中的多个形状需要显示相同的图像,即使在保留其唯一数据(名称,ID,大小,可见性,标题等)时也是如此。 The "swap" trick on that is prohibitive - I'd have to create fifty copies of every single "library" object, one for each instance in the UI that might need to take on that appearance. 对此的“交换”技巧是令人望而却步的 - 我必须创建每个“库”对象的五十个副本,一个用于UI中可能需要采用该外观的每个实例。


DESIDERATUM: 迫切要求:

So what I'd like to do is to simply have one library copy of those pictures, and be able to copy that image into the picture objects that are integrated into the UI. 所以我想做的就是简单地拥有这些图片的一个库副本,并能够将该图像复制到集成到UI中的图片对象中。


PROBLEM: 问题:

I can't find how to do this. 我找不到怎么做。

I can't find any field within the VBA data structures which corresponds to the "image" part of a picture object. 我在VBA数据结构中找不到与图片对象的“图像”部分相对应的任何字段。

CopyPicture() and then Paste() appears to work only when the destination for Paste() is an address (eg Range("A1")), and then it creates a new picture object. 只有当Paste()的目标是地址(例如Range(“A1”))时,CopyPicture()然后Paste()才会起作用,然后它会创建一个新的图片对象。 But I don't want a new object, and I don't want to paste into a cell. 但我不想要一个新对象,我不想粘贴到一个单元格中。 I want to overwrite the image data of an existing picture object with the image data of another picture object. 我想用另一个图片对象的图像数据覆盖现有图片对象的图像数据。

Duplicate() and similar routines are variations on the above - they create a new picture object. Duplicate()和类似的例程是上面的变体 - 它们创建了一个新的图片对象。 I've got particular events etc tied to the existing object, the destination object, whose image data I'm trying to change, so I have to change its image, not replace it with some newly created object every time. 我有特定事件等绑定到现有对象,目标对象,我正在尝试更改其图像数据,所以我必须更改图像,而不是每次都用一些新创建的对象替换它。

LoadPicture() - which goes via the hard drive rather than the clipboard (slower and, thus, less desirable) doesn't seem to work on shape objects, only OLEObjects. LoadPicture() - 通过硬盘驱动器而不是剪贴板(较慢,因此,不太理想)似乎不适用于形状对象,只有OLEObjects。 Would I have to change all those picture objects into ActiveX images? 我是否必须将所有图片对象更改为ActiveX图像? That's a whole lot of overhead for static pictures! 对静态图片来说,这是一大笔开销! And I expect all that disk reading & writing will bog down the sheet noticably. 而且我希望所有磁盘读写都会明显地陷入困境。

ActiveX - I could change these picture objects into activeX image objects, then change the 'library' objects, and then use an assignement (Shapes("Name").DrawingObject.Object.Picture = Shape("Library").DrawingObject.Object.Picture). ActiveX - 我可以将这些图片对象更改为activeX图像对象,然后更改'library'对象,然后使用一个assignement(Shapes(“Name”)。DrawingObject.Object.Picture = Shape(“Library”)。DrawingObject.Object 。图片)。 [This approach is along the lines of the approach here - Excel VBA: Copying Pictures from image controls to activeX objects ] I'm not sure I can make that work for my situation. [这种方法与此处的方法一致 - Excel VBA:将图片从图像控件复制到activeX对象 ]我不确定我是否可以根据我的情况进行操作。 Not only have I already designed much of the UI to take advantage of characteristics and functionality which Shape/Pictures have and OLEObjects do not (such as 3d formatting and the particular way 'fill' works on a picture object), but also manipulating OLEObjects is markedly slower than manipulating simple pictures, enough, in my implementation, to be frustrating to the user. 我不仅已经设计了大量的UI来利用Shape / Pictures所具有的特性和功能,而OLEObjects没有(例如3d格式化和'fill'在图片对象上的特定方式),而且还操纵OLEObjects是显着慢于操作简单的图片,足够的,在我的实现,是令人沮丧的用户。 (That's the one of the main reasons I switched away from using OLEObjects in an earlier stage of development.] (这是我在开发的早期阶段转而使用OLEObjects的主要原因之一。


Essentially, I want to do the equivalent of the "Change Picture" command one gets when right-clicking on a picture object (except getting the image data from another picture object or, if necessary, the clipboard, not from a file on disk). 基本上,我想做一个相当于右键单击图片对象时获得的“更改图片”命令(除了从另一个图片对象获取图像数据,或者,如果需要,从剪贴板获取图像数据,而不是从磁盘上的文件获取) 。 But when I record a macro of that action it comes up empty - apparently "Change Picture" doesn't produce any macro code. 但是,当我录制该动作的宏时,它会变空 - 显然“更改图片”不会产生任何宏代码。

A whole bunch of web searching and documentation browsing hasn't turned up anything else. 大量的网络搜索和文档浏览没有出现任何其他问题。


um.... help? 嗯....帮忙?

I hate to say this, but I think you have painted yourself into a corner on this one. 我讨厌这样说,但我认为你已经把自己描绘成了一个角落。 Excel's objects do not support modifying picture data in the way you describe, which means that you cannot make these changes while the sheet is open. Excel的对象不支持以您描述的方式修改图片数据,这意味着您无法在工作表打开时进行这些更改。 Even if you figured out how to make changes to the raw file, it does not satisfy your UI requirements. 即使您想出如何更改原始文件,它也不能满足您的UI要求。

Your workaround is probably your best (Excel-based) answer. 您的解决方法可能是您最好的(基于Excel)答案。

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

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