Consulting

Results 1 to 19 of 19

Thread: CopyPicture method of Range class failed

  1. #1

    CopyPicture method of Range class failed

    I'm using Excel 2010 (1x.0.???), 2013 (15.0.4797.1003) and Excel 2016 (16.0.6568.2025) on three independent PCs.

    For ease in reproducing the issue, I start with a blank worksheet, and enter "1" into cell A1 of Sheet1. I then create this macro:

    Sub Test()
    For x = 1 To 100
    Range("A1:A1").CopyPicture
    Next x
    End Sub
    When I run macro Test in Excel 2010, it never halts with an error... x always reaches 100.

    When I run macro Test in Excel 2013 or 2016, it halts with "CopyPicture method of Range class failed". x has a random value, but somewhere between 5 and 10 is typical. A breakpoint at Next x doesn't help, so I rule out a timing issue.

    BUT THIS ONLY STARTED TO OCCUR in Excel 2013 and 2016 in the past week. The real macro is much more complex, but the trivial macro leads me to think some auto update of Excel has suddenly broken CopyPicture!

    Our company relies on this method many times in a day. Any ideas would be greatly appreciated.
    Last edited by Aussiebear; 11-24-2016 at 07:10 AM. Reason: Added code tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Using Win10, 64 bit, Excel 16.0.6568.2025 it seems to work ok with a new empty workbook

    Capture.JPG


    You might try the Code Cleaner to 'refresh' your macros. It often fixes that sort of problem

    http://www.appspro.com/Utilities/CodeCleaner.htm
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    I changed my macro to read precisely like yours. My immediate window shows after over 3 tries it ran 3, 8 and 5 iterations.

    It's helpful to know that you're running the same build without seeing the issue. I'll try it on some additional desktops here. I can also try repairing my installation of Excel, but with it occurring on several desktops I'm not hopeful this will work.

    Thanks for the tip on CodeCleaner, but if I can't get this trivial macro to execute properly the problem must lie elsewhere.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Good luck tracking the problem down

    2. CodeCleaner actually works on all the modules in the workbook, not just a simple macro

    During the process of creating VBA programs a lot of junk code builds up in your files. If you don't clean your files periodically you will begin to experience strange problems caused by this extra baggage. Cleaning a project involves exporting the contents of all its VBComponents to text files, deleting the components and then importing the components back from the text files.

    3. Did you try opening a brand new WB, and just using the simple macro all by itself to see it the issue still persist?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    This is a new WB. I'm at a loss to understand why the problem only started to appear a few weeks ago, first in Excel 2013 and then Excel 2016. My theory was a recent update to Excel broke something, but then you tested on the same build and it's working.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by gmerrick View Post
    This is a new WB. I'm at a loss to understand why the problem only started to appear a few weeks ago, first in Excel 2013 and then Excel 2016. My theory was a recent update to Excel broke something, but then you tested on the same build and it's working.
    Try to 'Repair' Office and see is the only idea I have left
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Possibly change the default printer to another one

    Printer drivers can be tricky also
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Just for fun I added a delay in the loop:

    Sub Test()
        Dim x As Long
        For x = 1 To 100
            Application.Wait (Now + TimeValue("0:00:01"))
            Debug.Print x
            Range("A1:A1").CopyPicture
        Next x
    End Sub
    Now it works fine, but takes 100 seconds to complete. :-( Does this provide any clue as to what's happening?
    Last edited by Aussiebear; 03-29-2017 at 07:50 PM. Reason: Added code wrappers

  9. #9
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    2
    Location
    Quote Originally Posted by gmerrick View Post
    Just for fun I added a delay in the loop:

    Sub Test()
    Dim x As Long
    For x = 1 To 100
    Application.Wait (Now + TimeValue("0:00:01"))
    Debug.Print x
    Range("A1:A1").CopyPicture
    Next x
    End Sub

    Now it works fine, but takes 100 seconds to complete. :-( Does this provide any clue as to what's happening?
    I was struggling with the very same issue than you.

    Well, I did this and it worked for me:

    I added this sentence:

    rgToPic.Copy

    immediately before of this sentence:

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

    and I have never had the error in CopyPicture method again.












  10. #10
    Quote Originally Posted by Cucho View Post
    I was struggling with the very same issue than you.

    Well, I did this and it worked for me:

    I added this sentence:

    rgToPic.Copy

    immediately before of this sentence:

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

    and I have never had the error in CopyPicture method again.











    Your fix worked for me! We've been putting up with this irritation for 9 long months, and I can't thank you enough! THANK YOU, THANK YOU, THANK YOU SO MUCH! :-) Greg

  11. #11
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    2
    Location
    You're welcome.
    So this solved your issue as well:
    Range("A1:A1").Copy
    Range("A1:A1").CopyPicture

    The error is very random and erratic. Depends on your computer.
    In my case, apparently a computer with x64 bits processor is able to skip it but not in the case of other one with x32 bits.
    Got me nuts for months as well.

  12. #12
    Quote Originally Posted by Cucho View Post
    You're welcome.
    So this solved your issue as well:
    Range("A1:A1").Copy
    Range("A1:A1").CopyPicture

    The error is very random and erratic. Depends on your computer.
    In my case, apparently a computer with x64 bits processor is able to skip it but not in the case of other one with x32 bits.
    Got me nuts for months as well.
    I have a number of x64 systems, and the problem will come and go at random. Some days all our systems will work, some days one or two will work. I thought perhaps it was the version of Excel, which auto updates from time to time under Office 365, but found identical versions acting differently. It is a bizarre problem and I'd given up being able to solve it. Now it's finally solved!

  13. #13
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    2
    Location
    Hi,
    I have the same problem, the case is that if what is executed line a line with F8 all correct, but when I run
    it normally fails every time a site and what is always the same code and data. I have tried your method but it still
    gives me error ... I am desperate
    This is my code:


    Sub Situacion_actual_comparativa_PPT(diapositiva As Integer)
    On Error Resume Next
        Set ppApp = GetObject(, "PowerPoint.Application")
        On Error GoTo 0
    Set ppSlide = ppApp.ActiveWindow.View.slide
        slide = diapositiva
    Sheets("9 - S.A. Propuesta").Visible = True
    ppApp.ActiveWindow.View.GotoSlide (slide)
        Set ppSlide = ppApp.ActivePresentation.Slides(slide)
    Range("J2").Select
        Range("J2").Copy
        Range("J2").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    ppSlide.Shapes.Paste.Select 
    With ppApp.ActiveWindow.Selection.ShapeRange
            .IncrementLeft -206
            .IncrementTop -45
        End With
    End sub

    Now the fault gives me in the line that is in red.

    Shapes.paste : Invalid request. Clipboard is empty or contains data which may not be pasted here.

    Thanks !!!
    Last edited by Aussiebear; 03-29-2017 at 11:49 PM. Reason: Added code wrappers

  14. #14
    I understand your frustration. Does this always fail or only sometimes? For me it's hit and miss, which makes it nearly impossible to troubleshoot. Did you try adding the delay loop? It will take longer to complete but at least it increases the chances the macro will run.

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,050
    Location
    @Antal, Have you tried removing the .Select from the line
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  16. #16
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    2
    Location
    I've tried everything and it fails. It is random 100%, sometimes it runs without problems, other times it fails every two lines ... there is no way, with F8 everything works.

  17. #17
    Quote Originally Posted by gmerrick View Post
    Did you try adding the delay loop? It will take longer to complete but at least it increases the chances the macro will run.
    Did your code work if you used DoEvents instead of a delay?

  18. #18
    Quote Originally Posted by Antal View Post
    Hi,
    I have the same problem, the case is that if what is executed line a line with F8 all correct, but when I run
    it normally fails every time a site and what is always the same code and data. I have tried your method but it still
    gives me error ... I am desperate
    This is my code:


    Sub Situacion_actual_comparativa_PPT(diapositiva As Integer)
    On Error Resume Next
        Set ppApp = GetObject(, "PowerPoint.Application")
        On Error GoTo 0
    Set ppSlide = ppApp.ActiveWindow.View.slide
        slide = diapositiva
    Sheets("9 - S.A. Propuesta").Visible = True
    ppApp.ActiveWindow.View.GotoSlide (slide)
        Set ppSlide = ppApp.ActivePresentation.Slides(slide)
    Range("J2").Select
        Range("J2").Copy
        Range("J2").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    ppSlide.Shapes.Paste.Select 
    With ppApp.ActiveWindow.Selection.ShapeRange
            .IncrementLeft -206
            .IncrementTop -45
        End With
    End sub

    Now the fault gives me in the line that is in red.

    Shapes.paste : Invalid request. Clipboard is empty or contains data which may not be pasted here.

    Thanks !!!
    I had an idea to use error trapping and it seems to work. I replaced all lines using CopyPicture with this construct:

    On Error Resume Next
    Cells(1, 1).CopyPicture 'THIS WAS THE ORIGINAL LINE
    If Err.Number <> 0 Then
    Cells(1, 1).CopyPicture
    End If
    On Error GoTo 0

    It seems when the first attempt to execute would raise an error the second attempt always works... so far at least. Hope this works for you!

  19. #19
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •