简体   繁体   中英

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). This is with simple pictures, not ActiveX OLEObjects.


SITUATION:

I'm on Windows 7, Excel 2010.

I've got a sheet with multiple picture objects in the 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). 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.


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.


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.

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. 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. 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. Would I have to change all those picture objects into ActiveX images? 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). [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. 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. (That's the one of the main reasons I switched away from using OLEObjects in an earlier stage of development.]


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. Even if you figured out how to make changes to the raw file, it does not satisfy your UI requirements.

Your workaround is probably your best (Excel-based) answer.

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