excel 对象“Shape”的方法“Copy”失败:运行时错误“-2147221040(800401 d 0)”

kyxcudwk  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(616)

当我试图在两个工作表之间复制和粘贴形状时,我得到了上述错误。简单地说:

ws1.Shapes(1).Copy
ws2.Paste

.Copy行上失败,并出现错误。
我似乎找不到任何参考任何MS文档。有人能给我一些文档或对如何解决有任何建议吗?有什么变通办法吗?

2vuwiymt

2vuwiymt1#

也遇到了同样的问题。到目前为止,没有明显的节奏或原因。即随机选择和复制形状的代码失败,并报告错误消息。
能够缩小一些方面:
a)似乎只发生在windows 10与office 365(其他系统,没有问题)
B)如果用户重新启动他们的系统并再次运行相同的代码,则一切正常。
编辑:经过进一步的测试和研究,得出的结论是Excel中的完全随机故障。
GWteB在Mr Excel也得出了同样的结论(由于广告饱和,链接被删除)。那里提出的观点解决了部分问题。然而,他们只经历了粘贴失败。而我看到复制和粘贴都失败了。
我已经实现了一个函数(扩展了GWteB的思想):

Private Function TransferShape(stMT$, ByVal spSp As Shape, ByVal rgSpTarget As Range) As Integer

    Dim inAttempts%, inMaxAttempts%, inErrType%

''' Selection here assumes paste to target range is in the Active sheet
    rgSpTarget.Select 

procRetry: inAttempts = 0: inMaxAttempts = 100
        
    Do: inErrType = 0
    
    ''' Yield to OS occasionally
        If inAttempts Mod 20 = 0 Then DoEvents

    ''' With local error handling: Attempt the copy and paste
    ''' Note: Recording failed to copy as error type 601, and failed to paste as error type 602
    '''       Though not currently acting any differently based on type
        On Error Resume Next: Err.Clear: spSp.Copy
        If Err <> 0 Then
            inErrType = 601
        Else
            ActiveSheet.Paste
            If Err <> 0 Then inErrType = 602
        End If
        On Error GoTo 0
    
    ''' No error type: Exit Do now (all done)
        If inErrType = 0 Then Exit Do
            
    ''' Attempt failed: Increment attempts and try again (until Max Attempts reached).
        inAttempts = inAttempts + 1
    Loop Until inAttempts = inMaxAttempts
    
''' Failed: Prompt user to keep trying or not
    If inErrType Then
        If MsgBox(Buttons:=69, Title:=stMT, Prompt:= _
            "Attempting to copy and paste a picture failed " & inMaxAttempts & " times." & vbLf & vbLf & _
            "Shall we try again?") = vbRetry Then GoTo procRetry
    End If

''' DONE: Clear local objects and return Error Type (if any)
    Set spSp = Nothing: Set rgSpTarget = Nothing: TransferShape = inErrType

End Function

执行上述操作后,所有形状复制和粘贴失败均停止。
有趣的是,也没有看到最大尝试达到。但进一步的测试是另一天。

相关问题