Forum

Excel create an ima...
 
Notifications
Clear all

Excel create an image from picture in a Worksheet

3 Posts
2 Users
0 Reactions
106 Views
(@keebellah)
Posts: 373
Reputable Member
Topic starter
 

Hi everyone,
I've got something that doesn't work like it should

Public Function Picture_2_File(myImage As Variant) As String
Dim Ans As String, sPath As String
Dim rng As Range
Dim oCht As ChartObject
Dim oPic As Object
On Error Resume Next
Kill ThisWorkbook.Path & "Temp.gif"
Set oPic = ThisWorkbook.Worksheets("INTRO").Shapes(myImage)
Err.Clear
oPic.Copy
sPath = ThisWorkbook.Path & "Temp.gif"
Set oCht = ActiveSheet.ChartObjects.Add(100, 0, oPic.Width, oPic.Height)
oCht.Chart.Paste
oCht.Chart.Export sPath
oCht.Delete
Set oCht = Nothing
Err.Clear
On Error GoTo 0
Picture_2_File = sPath
End Function

The worksheet where the images are stored is named "INTRO" and i've got pictures

myImgae will contain the picture name of the one that I want to create as image file to import in a Userform (I chose for gif as extension but the same happens with jpg or bmp)

If I run the code the temp.gif is created but the content is blank

If I run the macro and interupt it at the before tha Paste command is exceuted and then go though it step by step (F8) it works and the temp.gif is the picture.
I tried adding a wait state (more than one) but that doesn't make it work.

Any suggestions? I'm using Excel 2021 and the worksheets are unportected etc etc.

Thanks in advance

 
Posted : 26/07/2023 4:26 am
(@debaser)
Posts: 838
Member Moderator
 

Try adding oCht.Activate before the Paste line.

 
Posted : 26/07/2023 7:10 am
(@keebellah)
Posts: 373
Reputable Member
Topic starter
 

And so simple, I knew it must be something like that. Cool

Great, thanks.Laugh

 
Posted : 26/07/2023 4:44 pm
Share: