VBA PasteSpecial Error for image

merce333

New Member
Joined
Jun 4, 2016
Messages
21
Hello. A cross-post disclaimer - I created a similar post here yesterday without much traction: https://social.msdn.microsoft.com/F...37308ee/vba-pastespecial-error?forum=exceldev
I will immediately put a link to this post in that Technet MS Forum thread, so that both places will see it is the same post so nobody wastes their time trying to resolve a (hopefully) solved issue.

I have this code which attempts to copy and paste a picture. It gives a sporadic (works error free about 3 / 10 times) error 1004, "PasteSpecial method of Range class failed". Often, if I just debug and click the "play" button to continue the code, it pastes just fine (although this only works sometimes. Others it gets hung up)
I followed EvoAndy and Siddharth Rout's exchange and tried the DoEvents solution, but it did not work: https://social.technet.microsoft.co...4781f98/vba-pastespecial-error?forum=exceldev

Other forums investigating this same issue say to have the code pause so that the Copy method has time to finish, and refer to Chip Pearson's suggestion of Application.Wait http://www.cpearson.com/Excel/WaitFunctions.aspx I added this into the code, but it also did not work.
Any help would be greatly appreciated! Thank you!
Code:
    ElseIf Not Intersect(Target, Range("Appraisers123")) Is Nothing Then
        Dim i, j        As Long
        Dim Arr(2, 1)   As Long
        Dim sVal        As String
        Dim sInitials   As String
        
        Dim rngQual     As Range
        
        Call ToggleAppSettings(False)
        'array of row,col pairs representing the paste destination in Range("rptCertSignatures")
        Arr(0, 0) = 1
        Arr(0, 1) = 1
        Arr(1, 0) = 1
        Arr(1, 1) = 3
        Arr(2, 0) = 6
        Arr(2, 1) = 1
        
        With Range("Appraisers123")
            For i = 1 To .Cells.Count
                sVal = .Cells(i, 1).Value
                With wsCert.Range("rptCertSignatures").Cells(Arr(i - 1, 0), Arr(i - 1, 1))
                    Call DeleteShapesInRange(.Cells(1, 1))
                    Set rngQual = wsQualifications.Range("Qual" & i & "Anchor")
                    'have to unhide or pics won't paste into correct spots
                    Call ToggleShowHideFormat(wsQualifications.Range("rptQual" & i), False)
                    Call DeleteShapesInRange(rngQual)
                    If sVal <> "" Then
                        'copy/paste signature
                        sInitials = wsInput.Range("Appraiser" & i & "initials").Value2
                        wsAppraisers.Shapes(sInitials & "Signature").Copy
                        DoEvents
                        On Error Resume Next
                        .PasteSpecial
                        While Err.Number <> 0 And j < 10
                            Err.Clear
                            DoEvents
                            Application.CutCopyMode = False
                            wsAppraisers.Shapes(sInitials & "Signature").Copy
                            Application.Wait Now + TimeSerial(0, 0, 1)
                            DoEvents
                            .PasteSpecial
                            j = j + 1
                            Debug.Print "paste " & j & ", err = " & Err.Number
                        Wend
                        On Error GoTo 0
                    End If
                End With
            Next
        End With
        Call ToggleShowHideFormat(wsCert.Range("rptCertification"), True)
        
        Application.CutCopyMode = False
        Target.Parent.Activate
        Call ToggleAppSettings(True, True)
        
        Set rngQual = Nothing

 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I've figured out a band-aid, yet repeatable solution. I closed out all my applications except Excel: Access, Outlook, Spotify, and a Chrome browser with 3 tabs open. The problem went away and works 100% of the time with no Debug.Print, so it never went into the error loop.
I then opened up 2 excel instances with 2 workbooks each, Outlook, Spotify, two windows explorer folders, an Access DB, and a Chrome browser with 10 tabs. The same sporadic problem came back. Worked sometimes, others not, and others it went into the error loop and succeeded after multiple attempts.
Finally, I closed out everything except Outlook, Spotify, just one excel instance with the workbook i'm testing, and 1 Chrome browser with 2 tabs, and not it works 100% of the time with no Debug.Print.

Thus, it seems there is some threshold resource level above which the clipboard behaves erratically and/or needs alot of time to perform the copy operation. I suppose this makes sense since the computer itself runs slower when many apps are open, just hadn't encountered anything like this before.
Is there a better way to handle this operation since I won't necessarily know how many resources will be available on the users' machines? Something like increase the Application.Wait time as the % CPU resources available goes down? That seems possible with an API, but my gut tells me 'probably don't want to over-complicate an issue that is resolved by closing extraneous applications."
Any input is appreciated, although I'm good to go for the time being! Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top