I have a VBA code which I am using to copy ranges as a picture and paste them into a chart. It does this so I can save it into a picture. This code has like a 70% success rate, and when it doesn't work, it gives out the error "CopyPicture method of range class failed". I don't understand why it can sometimes work and sometimes doesn't given that it is taking the same inputs.
Can anyone help?
Public Sub ExportRange(workbookPath As String, sheetName As String, rangeString As String, savepath As String)
Set tempWorkBook = Workbooks.Open(workbookPath)
Dim selectRange As range
Set selectRange = Worksheets(sheetName).range(rangeString)
Dim numRows As Long
numRows = selectRange.Rows.Count
Dim numCols As Long
numCols = selectRange.Columns.Count
' Transfer selection to a new sheet and autofit the columns
selectRange.Copy
Dim tempSheet As Worksheet
Set tempSheet = Sheets.Add
tempSheet.range("A1").PasteSpecial xlPasteAll
ActiveSheet.UsedRange.Columns.AutoFit
Set selectRange = ActiveSheet.UsedRange
selectRange.Select
selectRange.CopyPicture xlScreen, xlPicture
Dim tempSheet2 As Worksheet
Set tempSheet2 = Sheets.Add
Dim oChtobj As Excel.ChartObject
Set oChtobj = tempSheet2.ChartObjects.Add( _
selectRange.Left, selectRange.Top, selectRange.Width, selectRange.Height)
Dim oCht As Excel.Chart
Set oCht = oChtobj.Chart
oCht.Paste
oCht.Export filename:=savepath
oChtobj.Delete
Application.DisplayAlerts = False
tempSheet.Delete
tempSheet2.Delete
tempWorkBook.Close
Application.DisplayAlerts = True
End Sub
Although this is an old post, maybe this will help someone. I was struggling with similar problem for a long time.
CopyPicture
failed (on some computers more often than others, but hard to replicate on my laptop) when I was copying the range that contained an embedded PNG picture. It only failed inApplication.Visible=0
mode,Application.Visible=1
worked fine (for my application it is mandatory to run Excel in invisible mode). Finally I found that I can reproduce the problem 100% of the times when run on a VM with 1 CPU. The following solution is weird, but seems to be solving my problem completely.Embedded PNG is a
Shape
in Excel API terms. I just needed to cycle through the shapes (not even doing anything) before callingCopyPicture
:My finding is somewhat similar to this solution, where
CopyPicture
was failing on a range with charts. In their case, activating workbook and range itself helped.Hypothesizing, it seems plausible that on a slow or heavily loaded computer Excel does "lazy processing" of the complex objects on a page, i.e. not rendering them until object is accessed in some way. One way to force rendering seems to run in
Visible=1
mode. Another way is to cycle through the objects. If this is the case, then it is a bug of Excel'sCopyPicture
implementation where it doesn't force complex objects to render before trying to copy. When copy method finds out rendering for the target range is not ready, it simply throws an error instead of forcing the range to render. Well, at least that's my theory.Usually people tend to add
application.screenupdating=false
everywhere, as a habit (and it's usually good).But in this case, Excel can't see the Range (properly) and thus can't copy it. I guess it internaly does something for it to work but because of bad coding or lag whatsoever, it doesn't work every time.
So , i checked that if you remove
application.screenupdating=false
just before thecopypicture
, it works, (even without and better than the clear clipboard / Rg.copy / appearence=xlPrinter/ solutions).here is an exemple of code i use (with over-protection agains bad copies) :
You can skip the parts you don't need (this one is a control, when button right, copies range into a label's picture on a userform.
EDIT : i have found a way to force excel to wait until the clipboard has a picture in it, because sometimes it's too fast:
For me I had similar problem and I could solve it by changing between
xlScreen
andxlPrinter
inselectRange.CopyPicture
I hope this helps
I was struggling with the very same issue than you and I think is nothing to do with our VBA code or lack of programming skills. The error it's too random.
Moreover, if after getting the error message I clicked DEBUG and pressed F8 to continue executing the code step by step, then I was able to skip the error. After the problematic line I pressed F5 to continue in normal execute mode.
Of course, the above is not a solution but reveals nothing wrong with my coding.
Well, I did this and it worked for me:
before this sentence,
I added this one:
and I never have had the error in
CopyPicture
method again.Looking for this issue in other places I found out some users were able to skip the error by introducing this sentence before the
CopyPicture
method:The only thing that worked for me was to add a delay BEFORE the CopyPicture method. We are tweaking it shorter as I type this, but I know a 50 ms delay was working fine:
I found a easy way to fix this issue with which I was struggling for a few months. I know this is a "BAD CODE" but it helped and worked perfect for me. In my case details were getting copied but the debug error window was populating. Hence I just skipped the debug window and my life became easier.
Fix is just add below code in front of the "copy" code in your VBA. This will sure fix this error.