I'Ve got a really strange error/break in my code when the line "ThisWorkbook.Sheets("PD").Pictures.Paste(Link:=False).Select" is executed.
The reason it is strange is that this error only happens sometimes? The first time the code runs through its first pass, sometimes i get the error sometimes not... The second time time i run the loop, i always get an error The third time, i think it usually passes?!
In all 3 cases, if it does decide to stop, I get an error 1004 MsgBox and the line is highlighted in the debugger but then if I close the error message box, change nothing and just click the Play button. The code just carries on happily with no further errors lol.
My full code is huge so I cant paste it all but im hoping if I just paste the function where the error occurs im hoping one of you guys could suggest something ive not looked for or tried!
The line cant be wrong because when I click play after it breaks, it works?
Function SaveRangeAsPicture(ImgName As String)
Call UnprotectAll
'PURPOSE: Save a selected cell range as a JPG file to computer's desktop
'SOURCE: www.thespreadsheetguru.com
Dim cht As ChartObject
Dim ActiveShape As Shape
ThisWorkbook.Sheets("Part Database").Select
ThisWorkbook.Sheets("Part Database").Shapes.Range(Array(ImgName)).Select
'Copy/Paste Cell Range as a Picture ***(THIS IS WHERE ERROR OCCURS, 2ND LINE DOWN)***
Selection.Copy
ThisWorkbook.Sheets("Part Database").Pictures.Paste(link:=False).Select
Set ActiveShape = ActiveSheet.Shapes(ActiveWindow.Selection.Name)
'Create a temporary chart object (same size as shape)
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=ActiveShape.Width, _
Top:=ActiveCell.Top, _
Height:=ActiveShape.Height)
'Format temporary chart to have a transparent background
cht.ShapeRange.Fill.Visible = msoFalse
cht.ShapeRange.Line.Visible = msoFalse
'Copy/Paste Shape inside temporary chart
ActiveShape.Copy
cht.Activate
ActiveChart.Paste
'Save chart to User's Desktop as JPG File
cht.Chart.Export "C:\Users\" & Environ("Username") & "\Desktop\" & ActiveShape.Name & ".jpg"
'Replace the Picture on the Userform With the one from the part database
Call Add_Dynamic_Image2(ActiveShape.Name)
'Delete temporary Chart
cht.Delete
'Delete temporary image file on desktop
Kill ("C:\Users\" & Environ("Username") & "\Desktop\" & ActiveShape.Name & ".jpg")
'Delete Active Shape
ActiveShape.Delete
End Function
Thanks in advance!
So the things ive tried already:
- Made sure the variable (ImgName) being passed to the function is correct, and it is.
- Ive put the lines DoEvents in various places to no avail.
- Ive tried writing the line where the error occurs in various different ways.
- Ive checked that the name of the Physical image object matches the variable ImgName and it does.
UPDATE - MORE INFO: What this part of the code is aiming to do is not to copy and paste a picture from a spreadsheet into another spreadsheet, but to save a picture as a .jpg file temporarily somewhere on the users PC so I can then dynamically change the picture on a userform and setting Userform.Image1.Picture to the temporary file....
Then it deletes the .jpg on the desktop and clears the charts from the excel workbook that I used to create the .jpg.