Error with copy/paste in excel 2010 VBA when trying to insert charts, ranges etc into word

前端 未结 4 1146
长发绾君心
长发绾君心 2021-01-18 16:39

In researching this error I\'ve come to the conclusion that it has to do with the clipboard not clearing like it should which wasn\'t an issue when we were using 2003 but is

相关标签:
4条回答
  • 2021-01-18 17:05

    I'm still testing my solution but it's different than the suggestions made and so far has worked flawlessly. Just a different approach:

    I add a label and surround my attempt to copy with an error handler...

    Pg1CopyAttempt:
        DoEvents
        shSomeSheet.Range("A1:G30").Copy
        On Error GoTo Pg1PasteFail
        WordApp.Selection.PasteExcelTable False, False, False
        On Error goto 0 'disable the error handler 
    

    I call the label "Pg1CopyAttempt" because I know that Excel, through no fault of my own, may fail to copy it to the clipboard. If Excel does let me down, I won't know until I try to paste. When I do try I get thrown to the error Handler (Pg1PasteFail) when the paste method fails. This sits at the bottom of my routine (after a straight exit sub or an exit routine).

    Pg1PasteFail:
    If Err.Number = 4605 Then ' clipboard is empty or not valid.
        DoEvents
        Resume Pg1CopyAttempt
     End If
    

    It would be better to test for an empty clipboard programatically than to rely on an error handler but the idea here is to force a loop so that it keeps trying to copy until it succeeds. The simpler loop with programatic testing of the clipboard could exploit Sean's ClipboardEmpty Function (above). DoEvents is still exploited but even with DoEvents the routines can fail and are just instructed to keep trying.

    0 讨论(0)
  • 2021-01-18 17:09

    This is the code I use:

    Private Declare Function apiOpenClipboard Lib "user32" Alias "OpenClipboard" (ByVal hWnd As Long) As Long
    Private Declare Function apiEmptyClipboard Lib "user32" Alias "EmptyClipboard" () As Long
    Private Declare Function apiCloseClipboard Lib "user32" Alias "CloseClipboard" () As Long
    Private Declare Function CountClipboardFormats Lib "user32" () As Long
    
    Function ClipboardEmpty() As Boolean
        ClipboardEmpty = (CountClipboardFormats() = 0)
    End Function
    
    Sub EmptyClipboard()
      If apiOpenClipboard(0&) <> 0 Then
        Call apiEmptyClipboard
        Call apiCloseClipboard
      End If
    End Sub
    

    the function ClipboardEmpty is a test. e.g. if clipboardempty then
    The sub EmptyClipboard will simply clear the clipboard

    0 讨论(0)
  • 2021-01-18 17:09

    I haven't confirmed this as a final solution, but the hang up seems to be when copying - The copied data never makes it onto the clipboard, so the code fails when attempting to paste. Clearing the clipboard of any previously copied data before initiating the next copy command seemed to help. I still am using the wait time shown above, but I seem to be able to get away with a shorter wait time using the following function to clear the clipboard:

    Application.CutCopyMode = False

    0 讨论(0)
  • 2021-01-18 17:14

    I started getting this error when I upgraded from 2003 to 2010, but the macro still worked in 2003.

    The tables were there, but I noticed that sometimes the copying didn't work. As this didn't ever happen in debug mode, I added 5 secs waiting time right before the copying.

    This makes the macro slower, but at least it works.

    0 讨论(0)
提交回复
热议问题