none
Word is unable to paste something it just had copied, ends with VBA error 4605 RRS feed

  • Question

  • Hello everybody,

    at a customer's site there are Word macros running since _decades_ just fine. One thing they do is copy the whole current document's Range (into the Windows clipboard), create a new document, and Paste in the new document's Range.

    Since a few weeks, the customer uses Office 365 (32 bit), and code like this fails:

    sourceRange.Copy()
    destinationRange.Paste()

    VBA stops with error 4605 on the Paste() call. We could somewhat help by inserting a DoEvents() between Copy() and Paste(). But this does not help always.

    What did change in Word's handling of the clipboard so that something copied just one statement before cannot be pasted?


    Best Regards, Stefan Falk

    Wednesday, July 17, 2019 5:01 PM

Answers

  • Hello everybody!

    The problem is found and there is a workaround: Turn off clipboard history ("Zwischenablage-Verlauf" in German)!

    The following little test macro runs just fine without clipboard history, but fails after just a few copy/paste iterations if clipboard history is turned on:

    Option Explicit
    
    Public Sub TestClipboard()
    
        Dim source As Document, target As Document, _
            text As String, _
            i As Integer
        
        For i = 1 To 1000
            text = text & "Dies ist ein Test. "
        Next
        Set source = Documents.Add
        source.Range.Text = text
        Set target = Documents.Add
        For i = 1 To 100
            Debug.Print i
            source.Range.Copy
            DoEvents
            target.Range.Paste
            DoEvents
        Next
        target.Close False
        source.Close False
        Debug.Print "Finished"
    
    End Sub
    

    So clipboard history seems to have at least one little problem.


    Best Regards, Stefan Falk

    • Marked as answer by Stefan Falk Wednesday, April 1, 2020 10:22 AM
    Wednesday, April 1, 2020 10:22 AM

All replies

  • Hi,

    This IT Professionals Word forum is for general questions and feedback related to Word. Your question is mainly related to using VBA in Word. To help you better resolve the issue, I will move the thread to Word for Developers Forum. You may get more helpful replies there.

    Thanks for your kind understanding.

    Best Regards,

    Herb


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Office 2019.

    Thursday, July 18, 2019 2:59 AM
  • Just a guess really, but either
     a. it is "just" a timing problem, in which case you may be able to solve the problem by detecting the error and repeating a DoEvents until the command succeeds, or
     b. it is *sometimes* because more recent versions of Word display more "blocking" messages/warnings than older versions and a Windows is failing to open as a consequence of that.

    I don't know what you could do if it is (b) except try to find out what warning or Word is popping up and trying to switch it off in the settings,

    But if it is (a), you could for example use

    Const maxPasteRetryCount As Long = 20
    Dim pasteRetryCount as Long
    
    pasteRetryCount = 0
    paste:
      DoEvents
      On Error Goto pasteproblem
      destinationRange.Paste()
      On Error Goto 0
    
    ' then further down have:
    
    pasteproblem:
    If Err.Number = 4605 Then
      If  pasteRetryCount >= maxPasteRetryCount then 
        msgbox "some suitable error message"
      Else
        pasteRetryCOunt = pasteRetryCount + 1
        Err.Clear
        Resume paste
      End If
    End If
    


    Alternatively, you might be able to avoid the Error checking approach and test the existence of destinationRange, e.g.

    Const maxRetryCount As Long = 20
    Dim retryCount as Long
    
    retryCount = 0
    Do While ( destinationRange Is Nothing) And ( retryCount < maxRetryCount )
      DoEvents
      retryCount = retryCount + 1
    Loop
    
    If (destinationRange Is Nothing) Then
     ' inform the user
    Else
      destinationRange.Paste
    End If



    Peter Jamieson

    Monday, July 22, 2019 4:16 PM
  • Hello everybody!

    The problem is found and there is a workaround: Turn off clipboard history ("Zwischenablage-Verlauf" in German)!

    The following little test macro runs just fine without clipboard history, but fails after just a few copy/paste iterations if clipboard history is turned on:

    Option Explicit
    
    Public Sub TestClipboard()
    
        Dim source As Document, target As Document, _
            text As String, _
            i As Integer
        
        For i = 1 To 1000
            text = text & "Dies ist ein Test. "
        Next
        Set source = Documents.Add
        source.Range.Text = text
        Set target = Documents.Add
        For i = 1 To 100
            Debug.Print i
            source.Range.Copy
            DoEvents
            target.Range.Paste
            DoEvents
        Next
        target.Close False
        source.Close False
        Debug.Print "Finished"
    
    End Sub
    

    So clipboard history seems to have at least one little problem.


    Best Regards, Stefan Falk

    • Marked as answer by Stefan Falk Wednesday, April 1, 2020 10:22 AM
    Wednesday, April 1, 2020 10:22 AM