0
votes

I need to execute VBA script while user is logged off Windows 10, i.e. script should be executed by schedule while lock screen. Script copy charts from every excel sheet and paste them on the last sheet.

When I executed the following code while user is logged on then script finished succesfully. When I executed the following code by task scheduller while lock screen then error occured "method copy of chartobject failed". I suppose that it's connected with impossibility to use clipboard on the windows 10 lock screen.

For Each rs In ThisWorkbook.Worksheets
   For Each ch In ActiveSheet.ChartObjects
      ch.Copy
      Sheets(Sheets.Count).Select
      Cells(i, 1).Select
      ActiveSheet.Pictures.Paste
   Next ch
   i = i + 39
Next rs
1
I'm guessing that part of the problem is that you're using ActiveSheet and Select - How to avoid using Select.Darren Bartrup-Cook
Also instead of ActiveSheet.ChartObjects you should use rs.ChartObjects otherwise the loop through the worksheets is pretty useless.Pᴇʜ
Yup I use rs.ChartObjects instead of ActiveSheet.ChartObjects but nothing changed. It's not clear to me how to replace ActiveSheet and Select to Ranges as in exampleAzat Gimadiev

1 Answers

2
votes

Try something like this to remove .Select and ActiveSheet statements.

For Each rs In ThisWorkbook.Worksheets
   For Each ch In rs.ChartObjects
      ch.Copy
      ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Cells(i, 1).PasteSpecial 
      i = i + 39 'should be in the inner loop (just in case)
   Next ch
Next rs