Save an image pasted in Excel spreadsheet through VBA

Asked

Viewed 10,425 times

1

The code below copies a strip of cells of a spreadsheet that is pasted as image in another spreadsheet, but I did not find a way to save it as an image file in a folder, or even select it to work with this image by VBA.

Range("D19:H25").Select

Application.CutCopyMode = False

Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

Sheets("Imagem").Activate

Range("A1").Select

ActiveSheet.Paste

'a instrução     Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
'coloca a imagem na área de transferência (imagem originada de uma faixa de células selecionadas)

'... como salvar esta imagem em uma pasta a partir da área de transferência?

'... ou como salvá-la se já estiver colada na planilha?

'... como colocar esta imagem em um objeto image, tem como?

If this pasted image (which is in the clipboard) could be saved in a folder, it could later be read to an object image of VBA.

If there is a way to take this image from the clipboard and place it directly in an image object, it would also solve, but anyway it is important to be able to save this image.

You can do it?

  • I edited your question to remove the complement... because it is unnecessary (you opened your other question because of that).

  • Okay, I’m learning to interact here... see the last comment I posted.

1 answer

2


A possible solution is the following:

1 - Use the object Shape to access directly the object of the image. The image name (in my example "Image 1") is the name you can give directly in Excel, in the existing field next to the toolbar:

inserir a descrição da imagem aqui

BTW, the minimalist image example I used was taken from here. :)

2 - Then copy the object to the clipboard. You can do as you have done, but using the object by name is easier because it does not depend on its placement in the spreadsheet.

3 - Finally, add a temporary graphic object in Excel, and paste the image into it. This "trick" is necessary for you to use the save as image of the graphic objects. (Very Ever, Watson!). The original source of this part is that reply from Soen.

Ready! Your image was written to disk in the requested file. Here’s an example of code:

Dim oImage As Shape
Dim oSheet As Worksheet
Dim oTemp As ChartObject
Dim oChartArea As Chart

Set oSheet = ActiveSheet
Set oImage = oSheet.Shapes.Item("Imagem 1")

oImage.CopyPicture

Set oTemp = oSheet.ChartObjects.Add(0, 0, oImage.Width, oImage.Height)
Set oChartArea = oTemp.Chart
oTemp.Activate
With oChartArea
    .ChartArea.Select
    .Paste
    .Export ("C:\Temp\Teste.jpg")
End With
oTemp.Delete
  • Luiz, excellent, I tried it manually and it worked, but the code lacks the declaration of the variables oTemp and oChartArea, because I use Option Explicit and they are required. Please, as I must declare them?

  • Hi. I edited the answer to include in the code the definitions I had forgotten. If the answer helped you, please consider accepting it.

  • Okay, but the image generated in the file was Shape’s own and not the image that was pasted onto it. I believe that the lines I added in the code above to copy the image on Shape must be wrong or missing something. Please check my second code.

  • The image that was saved in the file is the image that is in the object Shape. As YOU YOURSELF ARGUED IN YOUR OTHER QUESTION (which I initially judged duplicated), this question is only about how to save an image in a file. There you want to know how to update an image with the contents of a selected range. Right?

  • It’s two problems with the same image. See, the user updates the picture in Excel, which, first, I need to copy and paste as image (this is ok), second I need to put this image in an image object to present in the system, without being in the spreadsheet, and at the same time, I need to save this image in a folder, because each project will have different images. That’s it.

  • Got it. Well, now it’s all right then. :)

  • Hi Luiz, not really, because I tested the presented solutions and could not save the image copied on the Shape file (saved the image of Shape itself) and put the copied image inside an image object. We evolved, but I still need these solutions.

  • Dude, I’m sorry, but I don’t understand your difficulty. The picture isn’t on a Shape. She is a Shape. The example I made here works exactly as illustrated (it saves the drawing of the sunglasses Stormtrooper in the file).

  • I’m sorry for my difficulty and I appreciate your commitment to help me. That’s exactly what I didn’t understand. I created a shape "rectangle" in Excel itself to give the name to Shape as you indicated (give the name manually). I believe that is my mistake. I want to take the image that I copied from a range of cells and that is in the clipboard to paste or generate this Shape and be able to save it as indicated. From what you’re saying, you can generate the image as Shape itself, which would solve my problem.

  • Ah, ok. It wasn’t supposed to do that. Take any image and paste it into Excel. It will immediately be a Shape. So name it. The codes of this and the other question work directly with the image. Is that in Excel, an image is also a Shape. :)

  • 1

    Okay. I get it now, it worked!!! Thank you very much, you solved a big problem!

  • Here’s what I found: replacing in the code "oImage.Copypicture" by "oImage.Select" the image is updated automatically, I think it would be worth it to add this to the reply.

  • What you "discovered" was a coincidence. How the code uses the command .Paste As a "trick" to record the image, you were probably lucky that the image was still in the Clipboard and so it worked. Test running this after you have copied any text pro Clipboard and you will see that it will save trash. The command .Select just selects the image object, and does not copy pro Clipboard (you accurate do!).

  • You are right, with the command oImage.Copypicture the image is not updated, but taking this command (without putting the other as well) is enough to update the image at the time of the copy, I do not understand the logic of it working like this in VBA, because the command you used makes all the sense.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.